Lab3 (B)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

LAB 3(2)

1. Write a program to enter a number and display whether its even or odd using
conditional operator.

#include <iostream>

using namespace std;

int main() {

int n;

cout<<"Enter your number"<<endl;

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>

using namespace std;

int main() {
int month;

cout<<"Enter the number of month"<<endl;

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:

cout<<"Enter a valid month";

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>

using namespace std;

int main() {

int day;

cout<<"Enter the number of day"<<endl;

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:

cout<<"Enter a valid day number";

break;

5. Write a C program to check whether a character is an alphabet, digit or special


character. (Use Ascii value)
#include <iostream>
using namespace std;
int main() {
char character;
cout<<"Enter your character"<<endl;
cin>>character;
int n= char (character);
cout<<"ASCII value of "<<character<<" is "<<n<<endl;
if((n>=33 && n<=47) || (n>=58 && n<=64) || (n>=91 && n<=96) || (n>=123 &&
n<=126)){
cout<<n<<" is a specail character";
}
else if(n>=48 && n<=57){
cout<<n<<" is a digit";
}
else if(n>=65 && n<=90){
cout<<n<<" is an upper case alphabet";
}
else if(n>=97 && n<=122){
cout<<n<<" is a lower case alphabet";
}
else{
cout<<"Can not identify";
}
}
6. Enter three sides of a triangle and check whether a triangle can be formed by the
given value for the angles. (Sum of three sides angles is 180)
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout<<"Enter the first angle"<<endl;
cin>>a;
cout<<"Enter the second angle"<<endl;
cin>>b;
cout<<"Enter the third angle"<<endl;
cin>>c;
if(a+b+c==180){
cout<<"Triangle can be formed";
}
else{
cout<<"Triangle can not be formed";
}
}

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>

using namespace std;

int main() {

float units, charge, surcharge;

long int id;

string name;

cout<<"Enter your name"<<endl;

cin>>name;

cout<<"Enter your id"<<endl;

cin>>id;

cout<<"Enter the units consumed"<<endl;

cin>>units;
if(units<=199){

charge=units*1.20;

else if(units>=200 && units<400){

charge=units*1.50;

else if(units>=400 && units<600){

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;

cout<<"The charge on the given units is "<<charge<<"rs";

8. Write a program in C which is a Menu-Driven Program to compute the area of the


various geometrical shape. (Menu means that a message should be displayed like
Enter 1 for circle, 2 for triangle, 3 for rectangle etc.)
#include <iostream>

#include <string>

using namespace std;

int main() {

int menu, area, radius, height, base, length, width;

const double pi=3.14;

cout<<"Enter the menu"<<endl;

cin>>menu;

if(menu==1){

cout<<"Enter the radius of circle"<<endl;

cin>>radius;

area=pi*(radius*radius);

cout<<"The area of the circle is "<<area<<"m^2";

else if(menu==2){

cout<<"Enter the height and base"<<endl;

cin>>height>>base;

area=(height*base)/2;

cout<<"The area of the triangle is "<<area<<"m^2";

else if(menu==3){

cout<<"Enter the length and width of the rectangle"<<endl;

cin>>length>>width;
area=length*width;

cout<<"Area of the rectangle is"<<area<<"m^2";

else{

cout<<"Enter a valid menu"<<endl;

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;
}

2. Enter 2 numbers and find whether first is a factor 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 factor of second number";
}
else{
cout<<"First number is not the factor of the seoncond number";
}
return 0;
}

3. Enter a number and find whether its even or odd.


#include <iostream>
using namespace std;
int main() {
int n;
cout<<"Enter your number"<<endl;
cin>>n;
if(n%2==0){
cout<<"Number is even";
}
else{
cout<<"Number is odd";
}
return 0;
}

4. Write a program to enter 2 numbers and print the larger in them.


#include <iostream>
using namespace std;
int main() {
int a, b;
cout<<"Enter your two number"<<endl;
cin>>a>>b;
if(a>b){
cout<<a<<" is largest number";
}
else{
cout<<b<<" is largest number";
}
return 0;
}

5. Enter 3 numbers and find largest and smallest in them.


#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout<<"Enter your three number"<<endl;
cin>>a>>b>>c;
if(a>b && a>b && b>c){
cout<<a<<" is largest number"<<endl;
cout<<c<<" is the smaleest number";
}
else if(a>b && a>c && c>b){
cout<<a<<" is largest number"<<endl;
cout<<b<<" is the smallest number"<<endl;
}
else if(b>a && b>c && a>c){
cout<<b<<" is the largest number"<<endl;
cout<<c<<" is the largest number"<<endl;
}
else if(b>a && b>c && c>a){
cout<<b<<" is the largest number"<<endl;
cout<<a<<" is the smallest number"<<endl;
}
else if(c>a && c>b && a>b){
cout<<c<<" is the largest number"<<endl;
cout<<b<<" is the smallest number"<<endl;
}
else if(c>a && c>b && b>a){
cout<<c<<" is the largest number"<<endl;
cout<<a<<" is the smallest number"<<endl;
}
else{
cout<<"Invalid Number"<<endl;
}
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;
}
}

10. An Insurance company follows following rules to calculate premium.


(1) If a person’s health is excellent and the person is between 25 and 35 years of age
and lives in a city and is a male then the premium is Rs. 4 per thousand and his
policy amount cannot exceed Rs. 2 lakhs.
(2) If a person satisfies all the above conditions except that the sex is female then the
premium is Rs. 3 per thousand and her policy amount cannot exceed Rs. 1 lakh.
(3) If a person’s health is poor and the person is between 25 and 35 years of age and
lives in a village and is a male then the premium is Rs. 6 per thousand and his policy
cannot exceed Rs. 10,000.
(4) In all other cases the person is not insured.
Write a program to output whether the person should be insured or not, his/her
premium rate and maximum amount for which he/she can be insured.
#include <iostream>
#include <string>
using namespace std;
int main() {
string gender;
string health;
string place;
int age;
cout<<"Enter the gender"<<endl;
cin>>gender;
cout<<"Enter your health"<<endl;
cin>>health;
cout<<"Enter your place"<<endl;
cin>>place;
cout<<"Enter the age"<<endl;
cin>>age;
if(gender=="male" && health=="excellent" && place=="city" && age>25 && age<35)
{
cout<<"Premium is Rs. 4 per thousand and policy amount cannot exceed Rs. 2
lakhs";
}
else if(gender=="female" && health=="excellent" && place=="city" && age>25 &&
age<35){
cout<<"Premium is Rs. 3 per thousand and policy amount cannot exceed Rs. 1 lakh. ";
}
else if(gender=="male" && health=="poor" && place=="village" && age>25 &&
age<35){
cout<<"Premium is Rs. 6 per thousand and policy cannot exceed Rs. 10,000";
}
else{
cout<<"Person is not insured";
}
}

11. Enter two numbers and find smaller in them.


#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout<<"Enter the two numbers"<<endl;
cin>>n1>>n2;
if(n1>n2){
cout<<n2<<" is the smallest";
}
else{
cout<<n1<<" is the smallest";
}
}

You might also like