Monday, April 16, 2012

C++ Code snippet for random number generation and guess quiz

Categories: ,

#include <iostream>
#include <cmath>

using namespace std;


int main()
{
    int p;
    int s;
    int z;
    int x = (p%100)*(s%100);
    if (x>8000){
        z = x/100;
    }
    else if(x>5000 && x<=8000){
        z = x/90;
    }
    else if(x>1000 && x<=5000)
    {   
        z = x/75;
    }
    else if(x>100 && x<=1000){
        z = x/35;   
    }
    else {
        z = x/10;
    }
    int opt;
    int n;
    cout<<"My Program generates a random number."<<endl;
    cout<<" Can you guess what it is?"<<endl;
    cout<<"You may take some help but remember these are limited.";
    cout<<endl;
    cout<<endl;   
    int count=0,count1=0,count2=0,count3 = 0;
   
    while(1){

        cout<<"1.Check the range in which the number can be"<<endl;
        cout<<"2.Check if the number is odd or even"<<endl;
        cout<<"3.check if the number is prime or not"<<endl;
        cout<<"4.Guess Your number (Only two time)"<<endl;
        cout<<"5.The number is ?"<<endl;
        cout<<"6.exit"<<endl;
        cout<<"Choose your option : "<<endl;
        cin>>opt;
        switch(opt){
            case 1 :
            {
            if(count==0){
                if (z<=50){
                    if(z <= 25){
                        if(z<=10){
                            cout<<"In range of 0 to 10"<<endl;
                        }
                        else{
                            cout<<"In range of 11 to 25"<<endl;
                        }
                    }
                    else if (z <=40){
                        cout<<"In range of 26 to 40";
                        }
                    else {
                        cout<<"in range of 41 to 50";
                        }
                    }
               
                else if(z<=60){
                    cout <<"In range of 51 to 60";
                    }
                else if (z <= 70){
                    cout <<"In range of 61 to 70";}
                else if (z <= 80){
                    cout <<"In range of 71 to 80";}
                else if (z <= 90){
                    cout <<"In range of 81 to 90";}
                else{
                    cout <<"In range of 91 to 99";
                }
            count = count+1;
            cout<<endl;
            }
            else {
                cout<<"You already had checked the range";
                cout<<endl;
            }
            }
            break;
            case 2:{
                if(count1 == 0){
                if(z % 2 == 0){cout<<"Number is even"<<endl;}
                else{cout<<"Number is odd"<<endl;}
                count1 = count1+1;}
                else{
                    cout<<"You already checked it"<<endl;
                }}
                break;
            case 3:{
                int f = (int)sqrt((double)z);
                int flag=1;
                for(int i=3; i<=f; i++ ){
                    if(z == 1 || z==0 ){
                        cout <<"Neither prime nor not prime";
                        }
                    else if(z == 2){
                        cout<<"Prime number";
                        }
                    if(z%i==0){
                        cout<<"Not prime";
                        flag = 0;
                        break;
                        }
                    }
                if(flag == 1){cout<<"Prime number";}
                cout<<endl;
                }   
            break;
            case 4:{
                if(count2 <= 2){
                    cout<<"Enter Number : "<<endl;
                    cin>>n;
                    if(n==z){
                    cout<<"You Won!!!!! Game exits!"<<endl;
                    count2 = count2 + 1;
                    return 0;
                    }
                    else{
                        count2 = count2 + 1;
                        cout<<"Incorrect guess. "<<"You have "<<(2-count2)<<" option left";}
                    if(count2 == 2){
                        cout<<"Game Over !!";
                        return 0;
                        }
                    cout<<endl;
                    }
                    }
                break;
            case 5: {
                cout<<endl<<"The number is "<<z<<" !"<<endl;
                break;
                }
           
            case 6:{
                cout<<"Thank you for playing my game :) "<<endl;
                break;
                return 0;
                }
            default :{
                cout<<"Please maintain the game rules and choose right option"<<endl;
                cout<<endl;
                break;
}
}

}

Spread The Love, Share Our Article

Related Posts

No Response to "C++ Code snippet for random number generation and guess quiz"

Post a Comment