#include<iostream.h>
main()
{
    int opt,x,y,fact,ans;
    for(ans=opt;ans!=0;ans++)
    { 
    cout<<"\n\t\t\t   *******MENU*******"
        <<"\n\t\t\t[1] FACTORIAL OF A NUMBER"
        <<"\n\t\t\t[2] PRIME NUMBER"
        <<"\n\t\t\t[3] ODD OR EVEN NUMBERS"
        <<"\n\t\t\t[4] MULTIPLICATION TABLE"
        <<"\n\t\t\tYOUR CHOICE: ";
    cin>>opt;

    switch(opt)
    {
    case 1:
        {
            cout<<"\nThis program tells you the Factorial of the number you have input...";
            cout<<"\nInput limit: ";
            cin>>y;
            fact=1;
            for(x=1;x<=y;x++)
            {
                fact=fact*x;
            }
            cout<<"\nThe factorial of the number you entered is "<<fact<<"."<<endl;
            break;
        }
    case 2:
        {
            cout<<"INPUT: ";
            cin>>x;
            for(y=2;y<=x/2;y++)
            {
                if(x%y==0)
                {
                    cout<<"Not Prime.";
                }
            }
            cout<<"Prime number.";
        break;
        }
    case 3:
        {
            cout<<"\nThis program will test if the number entered is an Odd or Even number...";
            cout<<"Input a number: ";
            cin>>x;
            if(x%2==0)
                cout<<"The number you entered is Even number.";
            else
                cout<<"Prime numberThe number you entered is Odd number.";
            break;
        }
    case 4:
        {
            for(x=1;x<=5;x++)
            {
                cout<<"\n";
            for(y=1;y<=5;y++)
                cout<<"\t"<<x*y;
            }
            cout<<endl;
            break;
        }
    default:
        cout<<"INVALID CHOICE!!!"<<endl;
    }
    cout<<"\n\t\t\tDo you wish to repeat?\n Press any number to Repeat the program Or press -1 to end..: ";
    cin>>ans;
    }
        cout<<endl;

    return 0;
}

Recommended Answers

All 4 Replies

Everything seems fine except your prime number checker.

Check this thread here

Check out firstPerson's function within his post.

>>can you check if my program code is right
Didn't you compile and run it to see for yourself?:?:

1) No Code tags

2)

int opt,x,y,fact,ans; // right now all variable has junk value
for(ans=opt;ans!=0;ans++)
//Read as ans = junkvalue, while junk is != 0, junk++

3) Didn't bother with the rest

I just copy pasted your code in and was looking for errors during runtime.

As firstPerson said, your for() loop is poorly done for keeping the program going till the user inputs -1 for quitting.

I would use a while() loop and put a bool variable done inside and if the user inputs -1 then done = true.

bool done = false;
int quit = 0;

while( !done )
{
	//.....
	cin>>quit;
	if( quit == -1 ? done = true : done = false );
}
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.