Close
Close

Practice questions on Decide if/else

Level 1

1.
Take values of length and breadth of a rectangle from user and check if it is square or not.
#include <iostream>
int main()
{
    using namespace std;

    int length,breadth;
    cout<<"Enter length"<<endl;
    cin>>length;
    cout<<"Enter breadth"<<endl;
    cin>>breadth;

    if(length==breadth)
    {
        cout<<"It is a square"<<endl;
    }
    else
    {
        cout<<"It is a rectangle"<<endl;
    }

    return 0;
}									

2.
Take two int values from user and print greatest among them.

3.
A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
#include <iostream>
int main()
{
	using namespace std;
	int quantity,price;
	cout << "Enter quantity" << endl;
	cin >> quantity;
	price = quantity*100;
	if (price>1000){
		cout << "Total cost is " << price-(price*.1) << endl;
	}
	else{
		cout << "Total cost is " << price << endl;
	}
	return 0;
}									

4.
A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years.
Ask user for their salary and year of service and print the net bonus amount.

5.
A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
#include <iostream>
int main()
{
	using namespace std;
	int marks;
	cout << "Enter marks" << endl;
	cin >> marks;
	
	if (marks < 25){
		cout << "F" << endl;
	}
	else if(marks >=25 && marks <45){
		cout << "E" << endl;
	}
	else if(marks >=45 && marks <50){
		cout << "D" << endl;
	}
	else if(marks >=50 && marks <60){
		cout << "C" << endl;
	}
	else if(marks >=60 && marks <80){
		cout << "B" << endl;
	}
	else if(marks >=80 && marks <100){
		cout << "A" << endl;
	}
	else{
		cout << "Invalid marks" << endl;
	}
	return 0;
}									

6.
Take input of age of 3 people by user and determine oldest and youngest among them.

7.
Write a program to print absolute vlaue of a number entered by user. E.g.-
INPUT: 1        OUTPUT: 1
INPUT: -1        OUTPUT: 1
#include <iostream>
int main()
{
    using namespace std;

    int x;
    cout<<"Enter a number"<<endl;
    cin>>x;
    if(x<0)
    {
        x = x*(-1);
    }

    cout<<"Absolute value is "<<x<<endl;


    return 0;
}									

8.
A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.

9.
Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.

10.
If
x = 2
y = 5
z = 0
then find values of the following expressions:
a. x == 2
b. x != 5
c. x != 5 && y >= 5
d. z != 0 || x == 2
e. !(y < 10)

11.
Write a program to check whether a entered character is lowercase ( a to z ) or uppercase ( A to Z ).

Level 2

1.
Write a program to check if a year is leap year or not.
If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.

2.
Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service.
if employee is female, then she will work only in urban areas.

if employee is a male and age is in between 20 to 40 then he may work in anywhere

if employee is male and age is in between 40 t0 60 then he will work in urban areas only.

And any other input of age should print "ERROR".

3.
A 4 digit number is entered through keyboard. Write a program to print a new number with digits reversed as of orignal one. E.g.-
INPUT : 1234        OUTPUT : 4321
INPUT : 5982        OUTPUT : 2895

Level 3

Ask Yours
Post Yours