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.

``````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;
do
{
clrscr();
cout<<"1. Prime "<<endl;
cout<<"2. Factorial "<<endl;
cout<<"3. Exit "<<endl;

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)
{
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)
{
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;
}``````
3
Contributors
3
Replies
4
Views
13 Years
Discussion Span
Last Post by sweety

Here is the modified program... Now this program will work fine even at the menu prompt, if u enter any number.. Except 1, 2, & 3, it will generate a error msg ...(Check out the default statement in main().

``````// 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;
do
{
clrscr();
cout<<"1. Prime "<<endl;
cout<<"2. Factorial "<<endl;
cout<<"3. Exit "<<endl;

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);
// CHANGE #1
default: cout<<"Invalid Choice Entered.. Try Again.."<<endl; break;

}
}
while(choice!=3);
}

void prime()
{
int n,flag = 0;

cout<<"Enter the number: ";
cin>>n;

if(n<=0)
{
getch();
// CHANGE #2
return ;
//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)
{
getch();
// CHANGE #3
return ;
//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();
// CHANGE #4
return ;
//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();
// CHANGE #5
return ;
//exit(0);
}
long int fact=1;

for(int i=1;i<=n;i++)
fact=fact*i;
cout<<endl<<"The factorial of "<<n<<" is"<<fact;
}``````

Hi Sweety,

remember that the reason your program was exiting when a user entered 0 or a negative number was because of:

``````{
getch();
exit(0);  //!
}``````

With the call to exit() removed the program continues in the switch statement. If the 0 or neg number is entered in the first overload of prime() the program does not return to the main menu because the second overload of prime() is called next in the switch statement.
If you want to make the program return to the main menu when 0 or negative is entered in the first call to prime(), consider changing the prime function to return an integer.
In the prime function use a 'return 1' statement if the invalid number is entered, and a 'return 0' if a valid number is entered.

In the switch staement, use the return value to determine whether to go on to the the second call to prime function or break to the menu:

``````switch(choice)
{
case 1:	cout<<endl<<" Prime Number checking  without parameters "<<endl;
if (prime() == 1)
{
break;
}
else
{
cout<<endl<<" Prime number checking with parameters"<<endl;
cout<<"Enter the number: ";
cin>>n;
prime(n);
getch();
break;
}``````

Thanks a lot for all the support.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.