Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

C++ program to select an operation from list and display its results

YASH PAL, 31 July 202422 August 2024

In this post, we will write a C++ program to select an operation from a list and display its results. the first user will select an operation from the list like Factorial, Odd/Even, Prime, Raise to power, and Square root. after that user will enter a number of his choice, and then the program will print the desired output.

C++ program to select an operation from list and display its results

C++ program to select an operation from the list and display its result

#include<iostream>
#include<conio.h>
#include<complex.h>
#include<stdlib.h>


int main()
{
 system("cls");

    int i,n,x,p,b,res,count;

    std::cout<<"1. Factorial "<<std::endl;
    std::cout<<"2. Odd/Even "<<std::endl;
    std::cout<<"3. Prime "<<std::endl;
    std::cout<<"4. Raise to power "<<std::endl;
    std::cout<<"5. Square root "<<std::endl;
    std::cout<<"Press the number of your choice ";
    std::cin>>n;

    res = 1;
    count = 0;

    switch(n)
    {
        case 1:
            {
              std::cout<<"Enter any number = ";
            std::cin >> n;
            for(i=1;i<=n;i++)
                res = res*i;
            std::cout<<"The factorial of given number is = "<<res<<std::endl;
        break;
            }


        case 2:
            {
             std::cout<<"Enter any number = ";
            std::cin>>n;
            if(n%2 == 0)
                std::cout<<"The number is even ";
            else
                std::cout<<"The number is odd ";
        break;
            }


        case 3:
           {
                std::cout<<"Enter any number = ";
            std::cin>>n;
            for(i=1;i<=n;i++)
                if(n%i == 0)
                    count = count + 1;
            if(count == 2)
                std::cout<<"The number is prime";
            else
                std::cout<<"The number is not prime";
        break;
           }

        case 4:
            {
                std::cout<<"Enter the base of number = ";
            std::cin>>b;
            std::cout<<"Enter the power of number = ";
            std::cin>>p;
            for(i=1;i<=p;i++)
                res = res*b;
            std::cout<<"The raise to power of number = "<<res;
        break;
            }

        case 5:
            {
                std::cout<<"Enter any number = ";
            std::cin>>n;
            float res = sqrt(n);
            std::cout<<"The square root of the number is = "<<res;
        break;
            }

        default:
            std::cout<<"Press the number in the above series";
    }

    getch();
}

Output

1. Factorial
2. Odd/Even
3. Prime
4. Raise to power
5. Square root
Press the number of your choice 3
Enter any number = 6
The number is not prime
coding problems cpp

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes