Wednesday, 10 April 2013
program to find factorial of number,number prime or not and number even or odd through functions
#include<iostream.h>
#include<conio.h>
using namespace std;
void fact(int);
void prime(int);
void even_odd(int);
main()
{
int number,num1,sum,num2,i=1;
while(i>0)
{
cout<<"\n1. FACTORIAL OF A NUMBER"<<endl;
cout<<"2. NUMBER IS PRIME OR NOT"<<endl;
cout<<"3. NUMBER IS EVEN OR ODD"<<endl;
cout<<"4. EXIT"<<endl;
cout<<"\n Enter Your Choice :"<<endl;
cin>>number;
switch(number)
{
case 1:
{
system("cls");
cout<<"\nfactorial of a number"<<endl;
cout<<"\nEnter Number :"<<endl;
cin>>num1;
fact(num1);
break;
}
case 2:
{
system("cls");
cout<<"\nNUMBER IS PRIME OR NOT"<<endl;
cout<<"\nEnter 1st Number :"<<endl;
cin>>num1;
prime(num1);
break;
}
case 3:
{
system("cls");
cout<<"\nNUMBER IS EVEN OR ODD"<<endl;
cout<<"\nEnter Number :"<<endl;
cin>>num1;
even_odd(num1);
break;
}
case 4:
{
exit(0);
}
i++;
}
}
getch();
}
void fact(int a)
{
int f=1,i=1;
while(i<=a)
{
f=f*i;
i++;
}
cout<<"\n"<<a<<"! = "<<f<<endl;
}
void prime(int num)
{
int i=2;
while(i<=num-1)
{
if(num%i==0)
{
cout << "\n" << num << " is not a prime number.\n";
break;
}
i++;
}
if(num==1)
{cout << "\n" << num << " is a prime number.\n";}
if(i==num)
cout << "\n" << num << " is a prime number.\n";
}
void even_odd(int e)
{
if(e%2==0)
{cout<<e<<" is even number"<<endl;}
else
{cout<<e<<" is odd number"<<endl;}
}
Labels:
programs
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment