Header Ad

C++ program to generate random number in circles and find out the sum

In this post, we are going to write a C++ program to generate random numbers in circles and find out the sum


C++ program to generate random numbers in circles and find the sum.

#include<iostream>
#include<conio.h>
#include<stdlib.h>

int main()
{
    int k1 = 0, sum1 = 0, p = 0;
    int k2 = 0, sum2 = 0, q = 0;
    int k3 = 0, sum3 = 0, r = 1;
    int k4 = 0, sum4 = 0, s = 1;
    char ch, choice = 'y';

    system("cls");
    while(choice != 'n')
    {
      system("cls");

        std::cout<<"Press i,l,k & j keys in circles"<<std::endl;
        ch = getch();

        switch(ch)
        {
            case 105:
            {
                if(p==0)
                {
                    k1 = rand()%10;
                    sum1 = k1+sum1;
                    std::cout<<"You press up arrow key"<<std::endl;
                    std::cout<<"The random number = "<<k1<<std::endl;
                    p++;
                    q--;
                }
                else
                    std::cout<<"Invalid key"<<std::endl;
            }
            break;

            case 108:
            {
                if(q==0)
                {
                    k2 = rand()%10;
                    sum2 = k2+sum2;
                    std::cout<<"You press right arrow key"<<std::endl;
                    std::cout<<"The random number = "<<k2<<std::endl;
                    q++;
                    r--;
                }
                else
                    std::cout<<"Invalid key"<<std::endl;
            }
            break;

            case 107:
            {
                if(r==0)
                {
                    k3=rand()%10;
                    sum3 = k3+sum3;
                    std::cout<<"You press down arrow key"<<std::endl;
                    std::cout<<"The random number = "<<k3<<std::endl;
                    r++;
                    s--;
                }
                else
                    std::cout<<"Invalid key"<<std::endl;
            }
            break;

            case 106:
            {
                if(s==0)
                {
                    k4 = rand()%10;
                    sum4 = k4+sum4;
                    std::cout<<"You press left arrow key"<<std::endl;
                    std::cout<<"The random number = "<<k4<<std::endl;
                    s++;
                    p--;
                }
                else
                    std::cout<<"Invalid key"<<std::endl;
            }
            break;

            default:
                std::cout<<"Invalid key"<<std::endl;
        }

        if(sum1 >= 20)
        {
            system("cls");
            std::cout<<"The up key win"<<std::endl;
            getch();
            exit(0);
        }
        if(sum2 >= 20)
        {
            system("cls");
            std::cout<<"The right key win"<<std::endl;
            getch();
            exit(0);
        }
        if(sum3 >= 20)
        {
            system("cls");
            std::cout<<"The down key win"<<std::endl;
            getch();
            exit(0);
        }
        if(sum4 >= 20)
        {
            system("cls");
            std::cout<<"The right key win"<<std::endl;
            getch();
            exit(0);
        }
        std::cout<<"Do you continue (Y/N) = ";
        choice = getche();
        std::cout<<std::endl;
    }
}



Output

Press i,l,k & j keys in circles
You press up arrow key
The random number = 5
Do you continue (Y/N) =

The righ key win

Post a Comment

0 Comments