IF Statement Programming Exercise P-3

What is IF Statement in C and C++ Programming

IF Statement Programming Exercise P-1 in C and C++

IF Statement Programming Exercise P-2

IF Statement Programming Exercise Part-4

IF Statement Programming Exercise Part-5

IF Statement Programming Exercise Part-6

Q.5 Write a program that inputs two values and finds out the greater value using if-else structure and displays result on screen.

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

main()
{

int a, b;
clrscr();
cout<<“Enter first value ? “;
cin>>a;
cout<<“Enter second value ? “;
cin>>b;
if (a>b)
cout<<“First value is greater”<<endl;
else
cout<<“Second value is greater”<<endl;
cout<<“End of the program”;
getch();
}

 

Q.6 Write a program that inputs a number and displays whether it is an even number
or odd number using logical operator “!” and if-else statement.

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

main()
{

int num;
clrscr();
cout<<"Enter any number ? ";
cin>>num;
if (!(num%2==0))
cout<<"Odd number"<<endl;
else
cout<<"Even number"<<endl;
getch();
}

Q.7 Write a program that inputs a single letter in a char variable. It displays “You are male” if ‘m’ is input and displays “You are female” if ‘f’ is input. It should display “Invalid input” if other letter is input.

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

main()
{

char ch;
clrscr();
cout<<"Enter a single character ? ";
cin>>ch;
if (!(ch=='m'|| ch=='f'))
cout<<"Invalid input"<<endl;
if (ch=='m')
cout<<"You are male"<<endl;
if (ch=='f')
cout<<"You are female"<<endl;
getch();
}

Q.8 Write a program that inputs a number and calculates the square root of the number if the number is greater than 0. Otherwise it displays a 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();
}

Q.9 Write a program that converts “Small Alphabet” into “Capital Alphabet” and vice versa.

#include<iostream.h>
#include<conio.h>
main()
{
char ch, res;
clrscr();
cout<<"Enter any alphabet ? ";
cin>>ch;
if(ch>='a' && ch<='z')

res = ch-32;

else
res = ch+32;
cout<<"Converted alphabet is : "<<res;
getch();
}

Q.10 Write a program that displays/prints the largest and smallest value entered by a user ten times in a single variable during the program execution.

#include<iostream.h>
#include<conio.h>
main()
{
int smallest, largest, count, num;
clrscr();
count = 1;
cout<<"Enter a number ? ";
cin>>num;
smallest = largest = num;
abc:
cout<<"Enter a number ? ";
cin>>num;
if(smallest > num)
smallest = num;
if (largest < num)
largest = num;
count++;
if(count < 10) goto abc;
// res = ch+32;
cout<<"Largest entered value is : "<<largest<<endl;
cout<<"Smallest entered value is : "<<smallest<<endl;
getch();
}

Q.11 A year is a leap year if it is divisible by 4, except that any year divisible by 100 is a leap year only if it is divisible by 400. Write a program that inputs a year such as 1996, 1800, and 2010, and displays “Leap year” if it is a leap year, otherwise displays “Not a leap year”.

#include<iostream.h>
#include<conio.h>
main()
{

int year;
clrscr();
cout<<"Enter a year ? ";
cin>>year;
if(year % 4 == 0 || year % 100 == 0 && year % 400 == 0)
cout<<"Leap year";
else
cout<<"Not a leap year";
getch();
}

Q.12 Write a program that inputs an integer value and finds out whether the integer is divisible by 3 or not using conditional operator.

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

 

Q.13 Write a program that inputs an integer value from the keyboard and finds out if the value is odd or even.

#include<iostream.h>
#include<conio.h>
main()
{
int x;
clrscr();
cout<<"Enter an integer value ? ";
cin>>x;
(x%2==0)? cout<<" Entered integer value is even": cout<<"Entered integer value is odd";
getch();
}

Q.14 Write a program using conditional operator to determine whether the year entered through keyboard is leap year or not.

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

Q.15 Write a program that inputs five values and finds out the greatest value using nested ‘if’ structure.

#include<iostream.h>
#include<conio.h>
main()
{
float n1, n2, n3, n4, n5, largest;
clrscr();
cout<<"Enter first value ? ";
cin>>n1;
cout<<"Enter second value ? ";
cin>>n2;
cout<<"Enter third value ? ";
cin>>n3;
cout<<"Enter fourth value ? ";
cin>>n4;
cout<<"Enter fifth value ? ";
cin>>n5;

if (n1>n2)
if (n1>n3)
if(n1>n4)
if(n1>n5)
largest = n1;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;
else
if(n3>n4)
if(n3>n5)
largest = n3;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;
else

if (n2>n3)
if(n2>n4)
if(n2>n5)
largest = n2;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;
else
if(n3>n4)
if(n3>n5)
largest = n3;
else
largest = n5;
else
if(n4>n5)
largest = n4;
else
largest = n5;

cout<<"Greatest value is : "<<largest;
getch();
}

Leave a Reply