Conditional Structure Programming Exercise Part-5

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-4

IF Statement Programming Exercise Part-6

 

Program 5.21:
Write a program that inputs the current reading and previous reading of electric meter. It finds out the total units consumed using nested ‘if-else’ structure and computes the electricity bill as follows:
// If units consumed are less than or equal to 300, then rate of electricity is Rs.12 per unit.
// If units consumed are more than 300 and less than or equal to 400, then rate of electricity is Rs.14 per unit.
// If units consumed are more than 400, then rate of electricity is Rs.20 per unit.

#include<iostream.h>
#include<conio.h>
main()
{
unsigned int previous, current, units, bill;
clrscr();
cout<<"Enter previous reading ? ";
cin>>previous;
cout<<"Enter current reading ? ";
cin>>current;
units = current - previous;
if (units > 400)
bill = units * 20;
else if (units> 300)
bill = units * 14;
else if (units <= 300)
bill = units * 12;
cout << "Total units :"<<units<<endl;
cout << "Electricity bill :"<<bill;
getch();
}

 

Program 5.22:
Write a program that inputs salary and computes the net salary. If the salary is 30000 or more, it deducts 13% of the salary. If the salary is 15000 or more but less than 30000, it deducts 1500 from the salary.

#include<iostream.h>
#include<conio.h>
main()
{
float netpay, deduct, salary;
clrscr();
cout<<"Enter salary? ";
cin>>salary;
if(salary >= 30000)
{
deduct = salary*13.0/100.0; 
netpay = salary - deduct; 
}
else if(salary >= 15000)
{
deduct = 1500; 
netpay = salary - deduct; 
}
else
netpay = salary; 
cout<<"Net pay = "<<netpay;
getch();
}

 

Program 5.23:
Write a program that inputs three numbers and finds out the largest number using logical operators and ‘if-else-if’ structure.

#include<iostream.h>
#include<conio.h>
main()
{
int x, y, z, max;
clrscr();
cout<<"Enter first number? ";
cin>>x;
cout<<"Enter second number? ";
cin>>y;
cout<<"Enter third number? ";
cin>>z;
if(x>y && x>z) 
max = x;
else if(y>x && y>z)
max = y;
else
max = z; 
cout<<"The Largest number is:"<<max;
getch();
}

 

Program 5.24:
Write a program that inputs any character through keyboard and determines whether it is a capital letter, small letter, a digit or a special symbol using logical operators and ‘if-else-if’ structure.

#include<iostream.h>
#include<conio.h>
main()
{
char ch;
clrscr();
cout<<"Enter any character ? ";
cin>>ch;
if(ch>='A' && ch<='Z') 
cout<<"It is a capital letter";
else if(ch>='a' && ch<='z') 
cout<<"It is a small letter";
else if(ch>='0' && ch<='9') 
cout<<"It is a digit";
else 
cout<<"It is a special symbol";
getch();
}

 

Program 5.25:
Write a temperature conversion program that gives the user the options for converting the Fahrenheit to Celsius or Celsius to Fahrenheit using “switch” statement. Then carry out the conversion. Interaction of the program might look like:
//1- To convert Fahrenheit to Celsius
//2- To convert Celsius to Fahrenheit
//Enter Option ?

#include<iostream.h>
#include<conio.h>
main()
{
double c, f;
int op;
clrscr();
cout<<"1- To convert Fahrenheit to Celsius\n";
cout<<"2- To convert Celsius to Fahrenheit\n";
cout<<" Enter Option ? ";
cin>>op;
switch(op)
{
case 1:
cout<<"Enter temperature in Fahrenheit ? ";
cin>>f;
c = 5.0/9.0*(f-32);
cout<<"Temperature in Celsius: "<<c;
break;

case 2:
cout<<"Enter temperature in Celsius ? ";
cin>>c;
f = 9.0/5.0*c+32;
cout<<"Temperature in Fahrenheit: "<<f;
break;
default:
cout<<"Invalid option";
}
getch();
}

 

Program 5.26:
Write a program that inputs an integer and displays the corresponding name of day of the week using switch selection structure. For example, if a user enters an integer 1, then program displays “Monday and displays ‘Tuesday” if user enters 2 and so on.

#include<iostream.h>
#include<conio.h>
main()
{
int n;
clrscr();
cout<<"Enter an integer value [1-7] ? ";
cin>>n;
switch(n)
{
case 1: cout<<"Monday"; break;
case 2: cout<<"Tuesday"; break;
case 3: cout<<"Wednesday"; break;
case 4: cout<<"Thursday"; break;
case 5: cout<<"Friday"; break;
case 6: cout<<"Saturday"; break;
case 7: cout<<"Sunday"; break;
default:
cout<<"Invalid number of day";
}
getch();
}

 

Program 5.27:
Write a program to input two values and operator from user, and perform arithmetic operation by making decision on operator using ‘switch’ statement.

#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;
switch(op)
{
case '+': cout<<n+m; break;
case '-': cout<<n-m; break;
case '*': cout<<n*m; break;
case '/': if(m==0)
cout<<"Division by zero!\n";
else
cout<<n/m; 
break;
default:
cout<<"Invalid operator";
}
getch();
}

 

Program 5.28:
Write a program to input a single character and print the message “ It is a vowel” if it is vowel otherwise print “It is a consonant”. Use the ‘switch’ selection structure.

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

Leave a Reply