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

Dani AI

Generated

The program exits because helper routines call exit() on bad input. That will terminate the whole process instead of returning control to the menu. As has shown, avoid calling exit() from inside your prime/fact helpers; let them return to main and let main decide whether to re-display the menu. s suggestion to return a status value from a helper is also useful: have the function indicate success/failure so main can skip follow-up prompts when appropriate.

A few practical hardening suggestions not in the thread:

  • Validate all user input, not just numeric range. If cin receives non-digits, check cin.fail(), call cin.clear() and cin.ignore() (or read a full line and parse it) so the menu loop doesn't spin on the same bad input.
  • Keep UI and logic separate. Make small functions that do one job: one that reads and validates a number, one that tests primality, one that computes factorial. Have each function return an error code or boolean instead of calling exit().

Correctness and portability notes:

  • For primality test, only test divisors up to the integer square root and skip even numbers after checking 2. For very large inputs consider a probabilistic or deterministic primality routine (e.g., Miller–Rabin) or a library implementation.
  • Factorials overflow fast. On typical 64-bit machines, 20! fits in 64 bits but 21! does not. Use unsigned long long/std::uint64_t with an explicit n<=20 guard, or use a big-integer library (Boost.Multiprecision) for larger values.
  • Avoid nonstandard headers/functions like conio.h, clrscr() and getch() if you want portable code; use int main() and return 0.

Checklist: remove exit() from helpers, validate input (including menu choices), return status from functions, limit factorial input or use big integers, and optimize the prime check. These steps will make the menu reliably reappear and the program more robust.

Recommended Answers

All 3 Replies

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;
	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);
                         // 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)
	{
	   cout<<"Please enter only positive numbers";
	   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)
	{
		cout<<"Please enter only positive numbers";
		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:

{
	   cout<<"Please enter only positive numbers";
	   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 the 0 or neg number is entered in the second overload then the program will return to the main menu.
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.