December 9, 2024

CONDITIONAL STATEMENT IN C++- 4 Important Questions

Hey guys, what is up. In this particular post, we will be talking about questions on a conditional statement.

Updated on (27/9/2021)

KEY POINTS

Ques1: To make a menu-driven calculator using a conditional statement.

#include<iostream.h>

#include<conio.h>

void main()

{

            clrscr();

            int n;

            float a,b,c;

cout<<“CALCULATOR \n 1.) ADDITION \n 2.) SUBTRACTION \n 3.) MULTIPLICATION \n 4.) DIVISION”;

            cout<<“\nENTER A = “;

            cin>>a;

            cout<<“ENTER B = “;

            cin>>b;

            cout<<“\nENTER YOUR CHOICE BETWEEN 1-4 = “;

            cin>>n;

            if(n<1 && n>4)

            {

                        cout<<“INVALID CHOICE”;

            }

            else if(n==1)

            {

                        c=a+b;

                        cout<<“ADDITION = “<<c;

            }

            else if(n==2)

            {

                        c=a-b;

                        cout<<“SUBTRACTION = “<<c;

            }

            else if(n==3)

            {

                        c=a*b;

                        cout<<“MULTIPLICATION = “<<c;

            }

            else

            {

                        c=a/b;

                        cout<<“DIVISION = “<<c;

            }

            getch();

}

OUTPUT:

output conditional statement 1
ouput conditional statement 2

Ques2: To print the grade of a student taking all the values from the user using a conditional statement.

#include<iostream.h>

#include<conio.h>

void main()

{

            clrscr();

            float english, maths, science, social, hindi, total, percentage;

            cout<<“ENTER VALUES OF ALL THE SUBJECTS: “;

            cout<<“\nENGLISH= “;

            cin>>english;

            cout<<“MATHS= “;

            cin>>maths;

            cout<<“SCIENCE= “;

            cin>>science;

            cout<<“SOCIAL= “;

            cin>>social;

            cout<<“HINDI= “;

            cin>>hindi;

            total=english + hindi + maths + social + science;

            percentage= (total/500) * 100;

            if (percentage>90 && percentage<=100)

            {

                        cout<<“\nGRADE A”;

            }

            else if (percentage>80 && percentage<=90)

            {

                        cout<<“\nGRADE B”;

            }

            else if (percentage>70 && percentage<=80)

            {

                        cout<<“\nGRADE C”;

            }

            else if (percentage>60 && percentage<=70)

            {

                        cout<<“\nGRADE D”;

            }

            else if (percentage>50 && percentage<=60)

            {

                        cout<<“\nGRADE E”;

            }

            else

            {

                        cout<<“\nFAIL”;

            }

            getch();

}

ouput conditional statement 5
ouput conditional statement 6

Ques3: To calculate the area and perimeter of circle, square, and rectangle using conditional statement.

#include<iostream.h>

#include<conio.h>

void main()

{

            clrscr();

            int option, side, length, breadth, radius;

            float area,perimeter;

            cout<<” AREA \n 1.) SQUARE \n 2.) RECTANGLE \n 3.) CIRCLE \n PERIMETER \n 4.) SQUARE \n 5.) RECTANGLE \n 6.) CIRCLE “;

            cout<<“\nENTER OPTION FROM 1 TO 6 WHICH YOU WANT TO PERFORM: “;

            cin>>option;

            if(option<1 && option>6)

            {

                        cout<<“\n INVALID OPTION”;

            }

            else if(option==1)

            {

                        cout<<“\n ENTER SIDE OF SQUARE= “;

                        cin>>side;

                        area= side*side;

                        cout<<“\n AREA OF SQUARE= “<<area;

            }

            else if(option==2)

            {

                        cout<<“\n ENTER LENGTH AND BREADTH OF RECTANGLE= “;

                        cout<<“\n LENGTH= “;

                        cin>>length;

                        cout<<“\n BREADTH= “;

                        cin>>breadth;

                        area= length*breadth;

                        cout<<“\n AREA OF RECTANGLE= “<<area;

            }

            else if(option==3)

            {

                        cout<<“\n ENTER RADIUS OF CIRCLE= “;

                        cin>>radius;

                        area= 3.14*radius *radius;

                        cout<<“\n AREA OF CIRCLE= “<<area;

            }

            else if(option==4)

            {

                        cout<<“\n ENTER SIDE OF SQUARE= “;

                        cin>>side;

                        perimeter= 4*side;

                        cout<<“\n PERIMETER OF SQUARE= “<<perimeter;

            }

            else if(option==5)

            {

                        cout<<“\n ENTER LENGTH AND BREADTH OF RECTANGLE= “;

                        cout<<“\n LENGTH= “;

                        cin>>length;

                        cout<<“\n BREADTH= “;

                        cin>>breadth;

                        perimeter= 2*(length+breadth);

                        cout<<“\n PERIMETER OF RECTANGLE= “<<perimeter;

            }

            else if(option==6)

            {

                        cout<<“\n ENTER RADIUS OF CIRCLE= “;

                        cin>>radius;

                        perimeter= 2*3.14*radius;

                        cout<<“\n PERIMETER OF CIRCLE= “<<perimeter;

            }

            getch();

}

ouput conditional statement 3
ouput conditional statement 4

QUES4: To calculate the tax a person has to pay based on his salary.

#include<iostream.h>

#include<conio.h>

void main()

{

     clrscr();

     float sal, tax;

     cout<<“ENTER SALARY = “;

     cin>>sal;

     if(sal<=10000)

     {

            cout<<“NO TAX”;

     }

     else if(sal>10000 && sal<=20000)

     {

           tax = (0.10*sal);

           cout<<“TAX = “<<tax;

     }

     else if(sal>20000 && sal<= 30000)

     {

           tax = (0.20*sal);

           cout<<“TAX = “<<tax;

     }

     else

     {

           tax = (0.25*sal);

           cout<<“TAX = “<<tax;

     }

     getch();

}

ouput conditional statement 7

In the upcoming post, you will be getting information related to technology and switch statement in C++. If you haven’t read about what is if else in c++ then go and read this. Also, stay tuned for the upcoming post

One thought on “CONDITIONAL STATEMENT IN C++- 4 Important Questions

Leave a Reply

Your email address will not be published. Required fields are marked *