Conditional Structure Programming Exercise Part-4

What is IF Statement in C and C++ Programming

IF Statement Programming Exercise Part-1

IF Statement Programming Exercise Part-2

IF Statement Programming Exercise Part-3

IF Statement Programming Exercise Part-5

IF Statement Programming Exercise Part-6

 

Program 5.11:
Write a program that inputs a year and finds whether it is leap year or not using if-else statement. A year is a leap year if it is divisible by 4.

#include<iostream.h>
#include<conio.h>
main()
{
int year;
clrscr();
cout<<"Enter a year ? ";
cin>>year;
if(year % 4 == 0)
cout<<year<<" is a leap year";
else 
cout<<year<<" is not a leap year";
getch();
}

 

Program 5.12:
Write a program that inputs two numbers and determines/tests whether the two numbers are equal or different, using if-else statement.

#include<iostream.h>
#include<conio.h>
main()
{
int x, y;
clrscr();
cout<<"Enter first number ? ";
cin>>x;
cout<<"Enter second number ? ";
cin>>y;
if(x == y)
cout<<"Both numbers are equal";
else
cout<<"Numbers are different";
getch();
}

 

Program 5.13:
Write a program that inputs two numbers and determines/tests whether the first number is a multiple of second number, using if-else statement.

#include<iostream.h>
#include<conio.h>
main()
{
int x, y;
clrscr();
cout<<"Enter first number ? ";
cin>>x;
cout<<"Enter second number ? ";
cin>>y;
if(x% y == 0)
cout<<"First number is a multiple of second ";
else
cout<<"First number is not a multiple of second ";
getch();
}

 

Program 5.14:
Write a program that inputs two numbers and finds larger number using conditional operator.

#include<iostream.h>
#include<conio.h>
main()
{
int x, y;
clrscr();
cout<<"Enter first number ? ";
cin>>x;
cout<<"Enter second number ? ";
cin>>y;
(x > y)? cout<<x<<" is greater" : cout<<y<<" is greater";
getch();
}

 

Program 5.15:
Write a program that inputs a number and displays whether the number is divisible by 5 or not using conditional operator.

#include<iostream.h>
#include<conio.h>
main()
{
int x;
clrscr();
cout<<"Enter a number ? ";
cin>>x;
(x%5==0)? cout<<x<< " is divisible by 5": cout<<x<<" is not divisible by 5";
getch();
}

 

Program 5.16:
Write a program that inputs three integer values and finds out if these values are equal or different using nested ‘if’ structure/statement.

#include<iostream.h>
#include<conio.h>
main()
{
int x, y, z;
clrscr();
cout<<"Enter first value ? ";
cin>>x;
cout<<"Enter second value ? ";
cin>>y;
cout<<"Enter third value ? ";
cin>>z;
if (x == y)
if(x == z)
cout<<"All values are equal";
else
cout<<"These values are different";
else
cout<<"These values are different";
getch();
}

 

Program 5.17:
Write a program that inputs three numbers or values and finds out the greatest value using nested ‘if’ structure.

#include<iostream.h>
#include<conio.h>
main()
{
float a, b, c, max;
clrscr();
cout<<"Enter first value ? ";
cin>>a;
cout<<"Enter second value ? ";
cin>>b;
cout<<"Enter third value ? ";
cin>>c;
if (a>b)
if(a>c)
max = a;
else
max = c;
else
if(b>c)
max = b;
else
max = c;
cout<<"Greatest value is : "<<max;
getch();
}

 

Program 5.18:
Write a program that finds out the grade of a student based on the average marks obtained in three subjects. The grade is calculated as;
// If average is greater than 80, grade is A.
// If average is less than 80 and greater than 60, grade is B.
// If average is less than 60 and greater than 33, grade is C.
// If average is less than 33, grade is F.

#include<iostream.h>
#include<conio.h>
main()
{
int s1, s2, s3, average;
char grade;
clrscr();
cout<<"Enter the marks of subject 1 ? ";
cin>>s1;
cout<<"Enter the marks of subject 2 ? ";
cin>>s2;
cout<<"Enter the marks of subject 3 ? ";
cin>>s3;
average = (s1 + s2 + s3)/3; 
if (average>33)
if(average>60)
if(average>80)
grade = ‘A’;
else
grade = ‘B’;
else
grade = ‘C’;
else
grade = ‘F’;
cout<<"Average marks =" <<average<<endl;
cout<<"Grade =" <<grade;
getch();
}

 

Program 5.19:
Write a program that inputs obtained marks of a student, calculates percentage (assume total marks are 1100), and displays his/her grade. The grade is found according to the following rules:
//Percentage Grade
//More than or equal to 80 A+
//Between 70 (inclusive) and 80 A
//Between 60 (inclusive) and 70 B
//Between 50 (inclusive) and 60 C
//Between 40 (inclusive) and 50 D
//Between 33 (inclusive) and 40 E
//Less than 33 F

#include<iostream.h>
#include<conio.h>
main()
{
float average, marks;
clrscr();
cout<<"Enter obtained marks ? ";
cin>>marks;
average = marks*100/1100.0;
cout<<"Average marks ="<<average<<endl;
if(average >= 80)
cout<<"A+";
else if(average >= 70)
cout<<"A";
else if(average >= 60)
cout<<"B";
else if(average >= 50)
cout<<"C";
else if(average >= 40)
cout<<"D";
else if(average >= 33)
cout<<"E";
else
cout<<"F";
getch();
}

 

Program 5.20:
Write a program that performs simple arithmetic calculations using ‘nested if-else’ structure.

#include<iostream.h>
#include<conio.h>
main()
{
float n, m;
char op;
clrscr();
cout<<"Enter first value, arithmetic operator and second value\n";
cin>>n>>op>>m;
if(op == '+')
cout<<n+m;
else if(op == '-')
cout<<n-m;
else if(op == '*')
cout<<n*m; 
else if(op == '/')
cout<<n/m; 
else
cout<<"Invalid operator";
getch();
}

Leave a Reply