mercredi 1 février 2017

Find whether a random number is Prime or not and Store in a character value

I am writing a game, where it should do below

Randomly generate a number. Check whether it is a Prime or not. Store "y" if its a Prime else "n"

I will ask the user whether it is prime or not. If he answers correctly I will proceed. Otherwise I will say you lost.

Code sample I tried is as follows:

cout<<"\n \n a no. of prime no.s will be displaed to you ,you will have to decide if it is prime or not within the given time\n \n ";
                       char ch1,ch2;
                       for(int i=1;i<=20;i++)
                        {
                            int a= rand( ) % 20;
                            cout<< "The number is:";
                            cout<<a;
                            for(int l=2;l<=a-1;l++)
                            {
                                if(a%l==0)
                                ch2='n';
                                else
                                ch2='y';

                            }

                            cout<<ch2;   
                            cout<<"\n\n Enter ""y"" If the number is Prime Else Please enter ""n""\n";                       
                            cin>>ch1;
                            if(ch1 == ' ')
                            {
                                cout<<"\n \n u lost";
                                break;
                            }
                           else if(ch1==ch2)
                           continue;
                           else if(ch1!=ch2)
                           {
                                cout<<"\n \n u lost";
                                break;
                           }

My code is not determining whether it is Yes or No. It is everytime storing "Y" only

Aucun commentaire:

Enregistrer un commentaire