Hi..this is a program to find if the no. is prime and to find the factorial of a given no.
i have validated this program ..so that it gives an error message and exits when negative nos. or zero are given as input.
Instead of exit..i want to display the menu that is....
cout<<"1. Prime "<<endl;
cout<<"2. Factorial "<<endl;
cout<<"3. Exit "<<endl;
...the above part of the program. How do i do this..plz help.
// a) to find whether a given no. is prime
// b) to find the factorial of a no.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void prime(),prime(int);
void fact(),fact(int);
void main()
{
int choice,n;
cout<<"\n Program Using Function Overloading \n\n ";
do
{
clrscr();
cout<<"1. Prime "<<endl;
cout<<"2. Factorial "<<endl;
cout<<"3. Exit "<<endl;
cout<<endl<<"Enter your choice :";
cin>>choice;
switch(choice)
{
case 1: cout<<" Prime Number checking without parameters "<<endl;
prime();
cout<<endl<<" Prime number checking with parameters"<<endl;
cout<<"Enter the number: ";
cin>>n;
prime(n);
getch();
break;
case 2: cout<<"Factorial of a number without parameters "<<endl;
fact();
cout<<endl<<" Factorial of a number with parameters"<<endl;
cout<<"Enter the number: ";
cin>>n;
fact(n);
getch();
break;
case 3: exit(0);
}
}
while(choice!=3);
}
void prime()
{
int n,flag = 0;
cout<<"Enter the number: ";
cin>>n;
if(n<=0)
{
cout<<"Please enter only positive numbers";
getch();
exit(0);
}
for(int i=2;i<=n/2;i++)
if(n % i == 0)
{
flag = 1;
break;
}
else
continue;
if(flag==0)
cout<<endl<<"The given no."<<n<<" is a prime no.";
else
cout<<endl<<"The given no."<<n<<" is not a prime no.";
}
void prime(int n)
{
if(n<=0)
{
cout<<"Please enter only positive numbers";
getch();
exit(0);
}
int flag=0;
for(int i=2;i<=n/2;i++)
if(n%i == 0)
{
flag=1;
break;
}
else
continue;
if(flag==0)
cout<<endl<<"The given no."<<n<<" is a prime no.";
else
cout<<endl<<"The given no. "<<n<<" is not a prime no.";
}
void fact()
{
int n,i;
cout<<endl<<" Enter the number:";
cin>>n;
if(n<=0)
{
cout<<"Valid only for positive numbers";
getch();
exit(0);
}
long int fact=1;
for(i=1;i<=n;i++)
fact=fact*i;
cout<<endl<<" The factorial of "<<n<<" is "<<fact;
}
void fact(int n)
{
if(n<=0)
{
cout<<"Valid only for positive number";
getch();
exit(0);
}
long int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
cout<<endl<<"The factorial of "<<n<<" is"<<fact;
}