24EEB0B59 ass 2
24EEB0B59 ass 2
24EEB0B59 ass 2
1. Write a C++ program to input electricity unit charges and calculate the total electricity bill
according to the given condition: For the first 50 units Rs. 0.50/unit For the next 100 units
Rs. 0.75/unit For the next 100 units Rs. 1.20/unit For units above 250 Rs. 1.50/unit An
additional surcharge of 20% is added to the bill.
#include <iostream>
using namespace std;
int main()
{
float unit ,total;
cout<<"enter the number of units used"<<endl;
cin>>unit;
if(unit<=50)
total=0.5 * unit;
if (unit>50 && unit<=150)
total=(0.5*50) + (unit-50)*0.75;
if(unit>150 && unit<=250)
total=(0.5*50) + (100 * 0.75) + (unit-150) *1.2;
if (unit >250)
total=(0.5*50) + (100 * 0.75) + (100*1.2) + (unit-250) *1.5;
total=total*1.2;
cout<<"the total electric charge is ="<<total<<endl;
return 0;
}
2. Write a program that will take three numbers from the keyboard and find the maximum of
these numbers. Then check whether the maximum number is even or odd.
#include <iostream>
using namespace std;
int main()
{int a,b,c,big;int d;
cout<<"enter three numbers"<<endl;
cin>>a>>b>>c;
big=a;
if(b>big )
big=b;
if(c> big)
{
big =c;}
d=big % 2;
if(d==0)
cout<<"the number is even";
if(d==1)
cout<<"the number is odd";
return 0;
}
3. Write a program that determines a student’s grade. The program will read three types of
scores (quiz, mid-term, and final scores) and determine the grade basedon the following
rules: -if the average score =90% =>grade=A -if the average score >= 70% and grade=B -if the
average score>=50% and grade=C -if the average scoregrade=F
# include<iostream>
using namespace std;
int main()
{
int q,m,f;float a;
cout<<"enter the marks in quiz out of 100"<<endl;
cin>>q;
cout<<"enter the marks in mid exams out of 100"<<endl;
cin>>m;
cout<<"enter the marks out of 100 in final exams"<<endl;
cin>>f;
a=(q+m+f)/3;
if(a>=90)
cout<<"grade = A";
if(a>=70 && a<90)
cout<<"grade = B";
if(a>=50 && a<70)
cout<<"grade = C";
if(a<50)
cout<<"grade = F ";
return 0;
}
4. A university has the following rules for a student to qualify for a degree with A asthe main
subject and B as the subsidiary subject: a. He should get 55 percent or more in A and 45
percent or more in B. b. If he gets less than 55 percent in A he should get 55 percent or more
in B. However, he should get at least 45 percent in A. c. If he gets less than 45 percent in B
and 65 percent or more in A he is allowedto reappear in an examination in B to qualify. d. In
all other cases he is declared to have failed.
#include <iostream>
int main()
int a,b;
cin>>a>>b;
else
cout<<"fail"<<endl;
return 0;
5. An insurance company insured their driver in the following case: - (i) Driver is married, (ii)
Driver is unmarried male and above 30 years (iii) Driver is unmarried female and above 25
years. In all other cases, the driver will not be insured. Write a program to check whether a
driver is insured or not based on marital status, sex, and age.
#include <iostream>
{int age,m,g;
char a;
cin>>m;
cin>>a;
g=a;
cout<<"enter age"<<endl;
cin>>age;
if (m==0)
cout<<"driver is insured";
cout<<"driver is insured";
else
return 0;
}
6.
#include <iostream>
int main()
{
char a;
int c;
cout<<"enter a character";
cin>>a;
c=a;
return 0;
}
2. An equation of the form ax^2+bx+c=0 is known as a quadratic equation. The
values of x that satisfy the equation are known as the roots of the equation.
#include <iostream>
#include <cmath>
int main()
float x,y;
float a,b,c,d;
cin>>a>>b>>c;
if((b*b)-(4*a*c)>=0)
x=(b-sqrt((b*b)-(4*a*c)))/2*a;
y=(b+sqrt((b*b)-(4*a*c)))/2*a;
}
if((b*b)-(4*a*c)==0)
{x=(b-sqrt(b*b-4*a*c))/(2*a);
y=(b+sqrt(b*b-4*a*c))/(2*a);
if((b*b)-(4*a*c)<0)
{d=abs((b*b)-(4*a*c));
return 0;
}
7 . In a company, worker efficiency is determined based on the time required for a
worker to complete a particular job. If the time taken by the worker is between
2 – 3 hours, then the worker is said to be highly efficient. If the time required by
speed. If the time taken is between 4 – 5 hours, the worker is given training to
improve his speed, and if the time taken by the worker is more than 5 hours,
then the worker has to leave the company. Write a program that calculates the
efficiency of worker by taking the time taken by the worker from the console.
#include<iostream>
int main()
float a;
cin>>a;
if(a>5)
return 0;
}
8. Write a C++ Program to check whether the triangle is equilateral, isosceles or
scalene.
#include <iostream>
int main()
float a,b,c;
cout<<"enter the value of first side"<<endl;
cin>>a;
cin>>b;
cin>>c;
return 0;
}
9. There are 5 types of tickets, each of which is denoted by a character (both
uppercase and lower case). Please find the equivalent strings for the characters.
D or d - Discount Ticket
V or v - VIP Ticket
S or s - Standard Ticket
C or c - Child Ticket
Write a program that takes the input of a character and prints the equivalent
string.
#include<iostream>
int main()
char a;
cin>>a;
if(a=='E' || a=='e')
cout<<"early bird ticket";
if(a=='D'|| a=='d')
cout<<"discount ticket";
if (a=='V'||a=='v')
cout<<"VIP ticket";
if(a=='S'||a=='s')
cout<<"standard ticket";
if(a=='C'||a=='c')
cout<<"child ticket";
}
10. Write a C++ program to design a calculator with basic operations using switch
statement.
#include <iostream>
int main()
char a;
int b,c,s,d,q,r,p;
cin>>a;
cin>>b>>c;
switch(a)
case '+':
s=b+c;
break;
case '-':
d=b-c;
break;
case'*':
p=b*c;
case '/':
q=b/c;
break;
case'%':
r=b%c;
break;
}return 0;
}
11. Write a menu-driven program (using a switch case) that computes the area of
geometrical shapes by getting the needed inputs from the user: 1. Area of the
circle; 2. Area of square; 3. Area of triangle. The program continues till the user
decides to exit.
#include <iostream>
#include <cmath>
g='a';
while( g!='e')
cin>>a;
switch(a)
{case 0:
cin>>r;
area=3.14*r*r;
break;
case 3:
int q,w,e;
cin>>q>>w>>e;
v=(q+w+e)/2;
area=sqrt(v*(v-w)*(v-e)*(v-q));
break;
case 4:
int s;
cin>>s;
area=s*s;
cout<<"enter the area of square"<<area<<endl;
break;
cin>>g;
if(g=='e')
cout<<"program exited"<<endl;
return 0;
}
12. Write a program to accept three numbers and display the following menu
#include <iostream>
int main()
cin>>d;
cin>>b>>c>>a;
switch(d)
case 1:
p=a*b*c;
break;
case 2:
s=a;
s=b;
s=c;
break;
case 3:
m=b;
m=c;
m=c;
m=a;
m=a;
break;
case 4:
bi=a;
bi=c;
bi=b;
break;
}return 0;}