Lab3 (B)
Lab3 (B)
Lab3 (B)
1. Write a program to enter a number and display whether its even or odd using
conditional operator.
#include <iostream>
int main() {
int n;
cin>>n;
n%2==0?cout<<"Even":cout<<"Odd";
return 0;
2. Write a program to enter 2 numbers and print the larger in them using conditional
operator.
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout<<"Enter your two number"<<endl;
cin>>n1>>n2;
n1>n2?cout<<n1<<" is larger":cout<<n2<<" is larger";
return 0;
}
3. Write a program to enter a number and display the name of month using switch
statement. If user enters an invalid number, then display message to enter a valid
number.
#include <iostream>
int main() {
int month;
cin>>month;
switch(month){
case 1:
cout<<"January";
break;
case 2:
cout<<"Feburary";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"November";
break;
case 12:
cout<<"December";
break;
default:
break;
}
4. Write a program to enter a number and display the name of the day using switch
statement (e.g., 1 = Monday, 2 = Tuesday, 9 = Invalid number). If user enters an
invalid number, then display message to enter a valid number.
#include <iostream>
int main() {
int day;
cin>>day;
switch(day){
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:
break;
7. Write a program in C to calculate and print the Electricity bill of a given customer.
The customer id., name and unit consumed by the user should be taken from the
keyboard and display the total amount to pay to the customer. The charge are as
follow
Unit Charge/unit
upto 199 @1.20
200 and above but less than 400 @1.50
400 and above but less than 600 @1.80
600 and above @2.00
If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum
bill should be of Rs. 100/-
#include <iostream>
#include <string>
int main() {
string name;
cin>>name;
cin>>id;
cin>>units;
if(units<=199){
charge=units*1.20;
charge=units*1.50;
charge=units*1.80;
else if(units>=600){
charge=units*2.0;
if(charge>=400){
surcharge=(charge*15)/100;
charge=charge+surcharge;
else if(charge<100){
charge=100;
#include <string>
int main() {
cin>>menu;
if(menu==1){
cin>>radius;
area=pi*(radius*radius);
else if(menu==2){
cin>>height>>base;
area=(height*base)/2;
else if(menu==3){
cin>>length>>width;
area=length*width;
else{
9. A triangle can be classified based on the lengths of its sides as equilateral, isosceles
or scalene. All 3 sides of an equilateral triangle have the same length. An isosceles
triangle has two sides that are the same length, and a third side that is a different
length. If all of the sides have different lengths, then the triangle is scalene. Write a
program that reads the lengths of 3 sides of a triangle from the user. Display a
message indicating the type of the triangle.
#include <iostream>
#include <string>
using namespace std;
int main() {
int side1, side2, side3;
cout<<"Enter the three sides of the triangle"<<endl;
cin>>side1>>side2>>side3;
if(side1==side2 && side1==side3 && side2==side3){
cout<<"Equilateral Triangle"<<endl;
}
else if((side1==side2 && side1!=side3) || (side1==side3 && side1!=side2) ||
(side2==side3 && side2!=side1)|| (side2==side1 && side2!=side3) || (side3==side1 &&
side3!=side2) || (side3==side2 && side3!=side1)){
cout<<"Isoceles Triangle"<<endl;
}
else if(side1!=side2 && side2!=side3 && side1!=side3){
cout<<"Scalene Triangle"<<endl;
}
else{
cout<<"Invalid Triangle";
}
}
LAB 3(1)
1. Enter 2 numbers and find whether first number is divisor of second or not.
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout<<"Enter your two numbers"<<endl;
cin>>n1>>n2;
if(n2%n1==0){
cout<<"First number is the divisor of second number";
}
else{
cout<<"First number is nit the divisor of the seoncond number";
}
return 0;
}
6. Any year is entered through the keyboard, write a program to determine whether
the year is leap or not. (A leap year is divided by 4, and not divisible by 100, with
exception of an year divisible by 400, e.g., 1700, 1800, 1900 are NOT leap , but 2000
is because of divisibility of 400)
#include <iostream>
using namespace std;
int main() {
int year;
cout<<"Enter the year"<<endl;
cin>>year;
if(year%400==0||(year%4==0 && year%100!=0)){
cout<<"Leap Year"<<endl;
}
else{
cout<<"Not Leap Year"<<endl;
}
return 0;
}
7. If the three sides of a triangle are entered through the keyboard, write a program to
check whether the triangle is valid or not. The triangle is valid if the sum of two
sides is greater than the largest of the three sides.
#include <iostream>
using namespace std;
int main() {
int side1, side2, side3;
cout<<"Enter the three sides of the triangle"<<endl;
cin>>side1>>side2>>side3;
if(side1>side2 && side1>side3 && side2+side3>side1){
cout<<"The triangle is valid"<<endl;
}
else if(side2>side1 && side2>side3 && side1+side3>side2){
cout<<"Triangle is valid"<<endl;
}
else if(side3>side1 && side3>side2 && side1+side2>side3){
cout<<"Triangle is valid"<<endl;
}
else{
cout<<"Triangle is invalid"<<endl;
}
return 0;
}
8. In a company, worker efficiency is determined on the basis of 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 the
worker is between 3 – 4 hours, then the worker is ordered to improve 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. If the time taken by the worker is input through the keyboard,
find the efficiency of the worker.
#include <iostream>
using namespace std;
int main() {
float time;
cout<<"Enter the time"<<endl;
cin>>time;
if(time>3 && time<4){
cout<<"Highly effecient";
}
else if(time>4 && time<5){
cout<<"Improve speed";
}
else if(time>5 && time<6){
cout<<"Training to improve speed";
}
else if(time>5){
cout<<"Leave the company";
}
else{
cout<<"Invalid time";
}
return 0;
}
9. A library charges a fine for every book returned late. For first 5 days the fine is 50
paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you
return the book after 30 days your membership will be cancelled. Write a program
to accept the number of days the member is late to return the book and display the
fine or the appropriate message.
#include <iostream>
using namespace std;
int main() {
int days;
cout<<"Enter the number of days"<<endl;
cin>>days;
if(days>=1 && days<=5){
cout<<"50 paisa fine";
}
else if(days>=6 && days<=10){
cout<<"1 rupee fine";
}
else if(days<10 && days<30){
cout<<"10 rupee fine";
}
else{
cout<<"Your membership is cancelled";
return 0;
}
}