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 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