IF Statement Programming Exercise Part-2

What is IF Statement in C and C++ Programming

IF Statement Programming Exercise Part-1

IF Statement Programming Exercise Part-3

Conditional Structure Programming Exercise P-4

Conditional Structure Programming Exercise P-5

Conditional Structure Programming Exercise P-6

Program 5.1:
Write a program that demonstrates the relational operators.

#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
cout<<"Enter any number ? "; 
cin>>n;
cout<<"Is n > 5 ? : Result is "<<(n>5)<<endl;
cout<<"Is n < 5 ? : Result is "<<(n<5)<<endl;
cout<<"Is n==5 ? : Result is "<<(n==5)<<endl;
getch();
}

 

Program 5.2:
Write a program that inputs a number and displays a message if the number is greater than 100.

#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
cout<<"Enter any number ? "; 
cin>>n;
if(n > 100)
cout<<"The Number is greater than 100"<<endl;
cout<<"Ok"; 
getch();
}

 

Program 5.3:
Write a program to test whether a given number is odd or even using simple ‘if’ structure.

#include<iostream>
using namespace std;
main()
{
int N;
cout<<"Enter an integer value ? ";
cin>>N;
if(N%2==0) cout<<"Even value "<<endl;
if(N%2==1) cout<<"Odd value "<<endl;
}

 

Program 5.4:
Write a program that inputs four numbers. It finds out the largest number and the smallest number using simple if statement.

#include<iostream.h>
#include<conio.h>
main()
{
float a, b, c, d, max, mini;
clrscr();
cout<<"Enter first value ? ";
cin>>a;
cout<<"Enter second value ? ";
cin>>b;
cout<<"Enter third value ? ";
cin>>c;
cout<<"Enter fourth value ? ";
cin>>d;
max = mini = a;
if(max < b) max = b;
if(max < c) max = c;
if(max < d) max = d;
if(mini > b) mini = b;
if(mini > c) mini = c;
if(mini > d) mini = d;
cout<<"\n The Largest value is : "<<max<<endl;
cout<<"The smallest value is : "<<mini;
getch();
}

 

Program 5.5:
Write a program that inputs an integer number and tests whether the number is divisible by 5 and 2 or not, using simple ‘if’ structure.

#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
cout<<"Enter an integer value ? ";
cin>>n;
if(n%5==0 && n%2==0)
cout<<"Number is divisible by 5 & 2 "<<endl;
getch();
}

 

Program 5.6:
Write a program that inputs two numbers and finds out whether the second number is square of the first number. Use simple if statement.

#include<iostream.h>
#include<conio.h>
main()
{
int num1, num2;
clrscr();
cout<<"Enter first number? ";
cin>>num1;
cout<<"Enter second number? ";
cin>>num2;
if(num1*num1==num2)
cout<<"Second number is square of first number";
getch();
}

 

Program 5.7:
Write a program that inputs a number and finds whether a number is odd or even using if-else statement.

#include<iostream>
using namespace std;
main()
{
int n;
cout<<"Enter an integer value ? ";
cin>>n;
if(n%2==0)
{
cout<<n<<“ is even number"<<endl;
}
else
{
cout<<n<<“ is odd number"<<endl;
}
cout<<"Ok";
}

 

Program 5.8:
Write a program that inputs salary and scale. It adds 40% of salary as increment if scale is greater than 16. Otherwise, it adds 50% of the salary as increment. The program displays the net pay.

#include<iostream.h>
#include<conio.h>
main()
{
float salary, netpay, incre;
int scale;
clrscr();
cout<<"Enter salary ? ";
cin>>salary;
cout<<"Enter scale ? ";
cin>>scale;
if(scale > 16)
incre = salary*40/100.0;
else
incre = salary*50/100.0;
netpay = salary + incre;
cout<<"Net Pay = "<<netpay;
getch();
}

 

Program 5.9:
Write a program that inputs a number. It calculates the square root of the number if it is greater than 0. Otherwise, program displays the message “Number is negative or zero”.

#include<iostream.h>
#include<conio.h>
#include<math.h>
main()
{
double res;
int num;
clrscr();
cout<<"Enter a number ? ";
cin>>num;
if(num > 0)
{
res = sqrt(num);
cout<<"Square root of "<<num<<" is = "<<res;
}
else
cout<<"Number is negative or zero ";
getch();
}

 

Program 5.10:
Write a program that inputs a character from user and checks whether it is vowel or consonant, using ‘if-else’ statement and OR operator.

#include<iostream.h>
#include<conio.h>
main()
{
char ch;
clrscr();
cout<<"Enter a single character ? ";
cin>>ch;
if(ch=='a'||ch =='A'||
ch=='e'||ch =='E'||
ch=='i'||ch =='I'|| 
ch=='o'||ch =='O'||
ch=='u'||ch =='U')
cout<<"It is a vowel"; 
else
cout<<"It is a consonant";
getch();
}

Leave a Reply