Download - Programming Lab Rep

Transcript
Page 1: Programming Lab Rep

Program No. 1Write a program that input radius of sphere from the user. Calculates its

volume and surface area using the formula Area=4πr2and circumference=4/3 πr2

where pi=3.14

#include<iostream.h>#include<conio.h>#define pi 3.14void main(){clrscr();float area,r,cir;cout<<"Enter radius of the sphere = ";cin>>r;area=4.0*pi*r*r;cir=4.0/(3.0*pi*r*r*r);cout<<"The Area of the sphere = "<<area<<endl;cout<<"The circumference of the sphere = "<<cir;getch();}

OUTPUT:

Page 2: Programming Lab Rep

Program No. 2:

Write a program that inputs miles from the user and convert miles into kilometers. One mile is equal to 1.609 kilometer.

#include<iostream.h>#include<conio.h>void main(){clrscr();float miles,km;cout<<"Enter distence in miles = ";cin>>miles;km=miles/1.609;cout<<"Distence in km is "<<km;getch();}

OUTPUT:

Program No. 3

Write a program that inputs age in years and displays age in days and months.

#include<iostream.h>#include<conio.h>void main(){clrscr();int year;unsigned int day,month;cout<<"Enter your age = ";

Page 3: Programming Lab Rep

cin>>year;day=year*365;month=year*12;cout<<"You are "<<day<<" days old"<<endl;cout<<"You are "<<month<<" months old";getch();}

OUTPUT:

Program No. 4Write a program that input total pages of a book, number of pages a

person reads in one day and numbers of days a person has read the book. It displays number of pages that have been and number of pages remaining.

#include<iostream.h>#include<conio.h>void main(){clrscr();int np,d,da,p,pr;float n;cout<<"Enter no. of pages = ";cin>>np;cout<<"Enter days in which you complite the book = ";cin>>d;cout<<"Enter how many days you read book = ";cin>>da;n=np/d;p=da*n;pr=np-p;cout<<"You read "<<p<<" pages"<<endl;cout<<pr<<" pages are remaining";getch();}

Page 4: Programming Lab Rep

OUTPUT:

Program No. 5

Write a program that input total number of students in a class and fee per student. It displays total fee collected from the class.

#include<iostream.h>#include<conio.h>void main(){clrscr();int student;unsigned long fee_per_student,total_fee;cout<<"Enter number of students in the class = ";cin>>student;cout<<"Enter fee per student = ";cin>>fee_per_student;total_fee=student*fee_per_student;cout<<"Total fee collected from the class is "<<total_fee<<" rupees";getch();}

OUTPUT:

Page 5: Programming Lab Rep

Program No. 6

Write a program that inputs a 3-digit number and displays its digits in separate three lines. For example if the user enters 123, the program displays the output as follows:

123

#include<iostream.h>#include<conio.h>void main(){clrscr();int n,a,b;cout<<"Enter 3-digit number = ";cin>>n;a=n/100;n=n%100;b=n/10;n=n%10;cout<<a<<"\n"<<b<<"\n"<<n;getch();}

OUTPUT:

Page 6: Programming Lab Rep

Program No. 7

Write a program to calculate the volume (V) of a cube by taking measures from the user.(Formula: V=length*width*height)

#include<iostream.h>#include<conio.h>void main(){clrscr();float l,w,h,v;cout<<"Enter length of cube = ";cin>>l;cout<<"Enter width of cube = ";cin>>w;cout<<"Enter height of cube = ";cin>>h;v=l*w*h;cout<<"Volume of a cube is "<<v;getch();}

OUTPUT:

Page 7: Programming Lab Rep

Program No. 8Write a program to swap the values of three variables with using fourth

variable.

#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b,c,d;cout<<"Enter value of a = ";cin>>a;cout<<"Enter value of b = ";cin>>b;cout<<"Enter value of c = ";cin>>c;d=a;a=b;b=c;c=d;cout<<"After swaping"<<endl;cout<<"a = "<<a<<endl;cout<<"b = "<<b<<endl;cout<<"c = "<<c;getch();}

OUTPUT:

Page 8: Programming Lab Rep

Program No. 9Write a program that input pounds from the user and converts it into

kilograms.

#include<iostream.h>#include<conio.h>void main(){clrscr();float pound,kg;cout<<"Enter mass Pounds = ";cin>>pound;kg=0.454*pound;cout<<"your mass is "<<kg<<" kilogram";getch();}

OUTPUT:

Program No. 10Write a program that reads a positive number and then computes the

logarithm of that value to the base 2.

#include<iostream.h>#include<conio.h>#include<math.h>void main(){clrscr();double result;int x;cout<<"Enter value of x = ";cin>>x;result=log(x)/log(2);cout<<"Log of "<<x<<" with base 2 is "<<result;getch();}

Page 9: Programming Lab Rep

OUTPUT:

Program No. 11Write a program that input 5-digit number through keyboard and

calculate the sum of its digits.#include<iostream.h>#include<conio.h>void main(){clrscr();int n,a,b,c,d,sum;cout<<"Enter 5-digit number = ";cin>>n;a=n/10000;n=n%10000;b=n/1000;n=n%1000;c=n/100;n=n%100;d=n/10;n=n%10;sum=a+b+c+d+n;cout<<"The sum of the 5-digit number is "<<sum;getch();}

OUTPUT:

Page 10: Programming Lab Rep

Program No. 12Write a program that inputs two times in hh:mm:ss format, adds both

times and displays the total time.

#include<iostream.h>#include<conio.h>void main(){clrscr();int h1,h2,h3,m1,m2,m3,s1,s2,s3;cout<<"Enter first time: "<<endl;cout<<"hours: ";cin>>h1;cout<<"minutes: ";cin>>m1;cout<<"second: ";cin>>s1;cout<<"Enter second time: "<<endl;cout<<"hours: ";cin>>h2;cout<<"minutes: ";cin>>m2;cout<<"second: ";cin>>s2;h3=h1+h2;m3=m1+m2;s3=s1+s2;cout<<"sum of the times is "<<h3<<":"<<m3<<":"<<s3;getch();}

OUTPUT:

Page 11: Programming Lab Rep

Program No. 13Write a program that inputs a number and display its corresponding ASCII

code.

#include<iostream.h>#include<conio.h>void main(){clrscr();char n;int a;cout<<"Enter a number = ";cin>>n;a=(int)n;cout<<"The ASCII code of "<<n<<" is "<<a;getch();}

OUTPUT:

Program No. 14Write a program that inputs marks obtained by a student in five subjects.

It then calculates and displays the total marks and percentage.

#include<iostream.h>#include<conio.h>void main(){clrscr();int eng,urdu,phy,bio,che,om;float per;cout<<"Enter marks in English out of 100 = ";cin>>eng;cout<<"Enter marks in Urdu out of 100 = ";cin>>urdu;

Page 12: Programming Lab Rep

cout<<"Enter marks in Physics out of 100 = ";cin>>phy;cout<<"Enter marks in biology out of 100 = ";cin>>bio;cout<<"Enter marks in chemistery out of 100 = ";cin>>che;om=eng+urdu+phy+bio+che;cout<<"Your obtained marks out of 500 are "<<om<<endl;per=(om/500.0)*100.0;cout<<"Your persentage is "<<per;getch();}

OUTPUT:

Program No. 15Senior salesperson is paid Rs.400 a week, and a junior salesperson is paid

Rs.275 a week. Write a program that accepts as input a salesperson’s status in the character variable status. If status is ‘s’ or ‘S’, the senior’s salary should be displayed; if status is ‘j’ or ‘J’, the junior person’s salary should be displayed, otherwise display error message.

#include<iostream.h>#include<conio.h>void main(){clrscr();char n;cout<<"Enter a code in charecter = ";cin>>n;if(n=='s'||n=='S')

Page 13: Programming Lab Rep

cout<<"Senior salesperson is paid Rs.400 a week ";else if(n=='j'||n=='J')cout<<"Jonior salesperson is paid Rs.275 a week ";elsecout<<"error";getch();}

OUTPUT:

Program No. 16Write a program that contains an if statement that may be used to

compute the area of a square (area= side*side) or a triangle (area=1/2*base*height) after promoting the user to type the first character of the figure name (s or t).

#include<iostream.h>#include<conio.h>void main(){clrscr();float square,triangle,base,height,side;char n;cout<<"Enter s for find area of square or Enter t for find area of triangle"<<endl;cout<<"Enter code = ";cin>>n;if(n=='s'){cout<<"Enter length of side = ";cin>>side;square=side*side;cout<<"The area of a square is "<<square;}else if(n=='t')

Page 14: Programming Lab Rep

{cout<<"Enter base of a triangle = ";cin>>base;cout<<"Enter height of a triangle = ";cin>>height;triangle=0.5*base*height;cout<<"The area of a triangle is "<<triangle;}getch();}

OUTPUT:

Program No.17Write a program that accept the code number as an input and display the

correct disk drive manufacture as follows:Code disk drive manufacture

1 Western Digital2 3M Corporation 3 Maxell Corporation4 Sony Corporation 5 Verbatim Corporation

#include<iostream.h>#include<conio.h>void main(){clrscr();int n;

Page 15: Programming Lab Rep

cout<<"code\tdisk drive manufacture\n1\tWestern Digital\n2\t3M Corporation\n3\tMaxell Corporation\n4\tSony Corporation\n5\tVerbatim Corporation"<<endl;

cout<<"Enter code = ";cin>>n;if(n==1)cout<<"The disk drive manufacture is Western Digital";else if(n==2)cout<<"The disk drive manufacture is 3M Corporation";else if(n==3)cout<<"The disk drive manufacture is Maxell Corporation";else if(n==4)cout<<"The disk drive manufacture is Sony Corporation ";else if(n==5)cout<<"The disk drive manufacture is Verbatim Corporation";elsecout<<"invalid code!";getch();}

OUTPUT:

Program No. 18Write a program that inputs a value and type of conversion. The program

should then output the value after conversion. The program should include the following conversions:1 inch = 2.54 centimeters1 gallon = 3.785 liters1 mile = 1.609 kilometers1 pound = .4536 kilograms

Page 16: Programming Lab Rep

Make sure that program accepts only valid choices for type of conversion to perform.

#include<iostream.h>#include<conio.h>void main(){clrscr();float value,cm,l,km,kg;int type;cout<<"Enter \n1 for inch to centimeter \n2 for gallon to liters\n3 for mile to kilometers\n4 for pounds to kilogram"<<endl;cout<<"Enter conversion type = ";cin>>type;if(type==1){cout<<"Enter value in inch = ";cin>>value;cm=value*2.54;cout<<"Answer is "<<cm<<" centimeters";}else if(type==2){cout<<"Enter value in gallon = ";cin>>value;l=value*3.785;cout<<"Answer is "<<l<<" liters";}else if(type==3){cout<<"Enter value in mile = ";cin>>value;km=value*1.609;cout<<"Answer is "<<km<<" kilometers";}else if(type==4){cout<<"Enter value in pound = ";cin>>value;kg=value*0.4536;cout<<"Answer is "<<kg<<" kilograms";}elsecout<<"Invalid code";getch();}

Page 17: Programming Lab Rep

OUTPUT:

Program No. 19

Write a program that inputs temperature and displays a message as follows:

Temperature MessageGreater than 35 Hot dayBetween 25 and 35 (inclusive)

Pleasant day

Less than 25 Cool day

#include<iostream.h>#include<conio.h>void main(){clrscr();float tem;cout<<"Enter temperature = ";cin>>tem;if(tem>=35)cout<<"Hot day";else if(tem>=25)cout<<"Pleasant day";else if(tem<25)cout<<"Cool day";getch();}

Page 18: Programming Lab Rep

OUTPUT:

Program No.19

Write a program that converts MILITARY time to STANDARD time.

#include<iostream.h>#include<conio.h>void main(){clrscr();int h1,h2,m1;cout<<"Enter Militery time 24 hours:"<<endl;cout<<"For example 13:50"<<endl;cout<<"Enter hours: ";cin>>h1;cout<<"nter Minutes: ";cin>>m1;if(h1<13)h2=h1;else if(h1==13)h2=1;else if(h1==14)h2=2;else if(h1==15)h2=3;else if(h1==16)h2=4;else if(h1==17)h2=5;else if(h1==18)h2=6;else if(h1==19)h2=7;else if(h1==20)h2=8;else if(h1==21)

Page 19: Programming Lab Rep

h2=9;else if(h1==22)h2=10;else if(h1==23)h2=11;else if(h1==00)h2=12;cout<<"Standard Time is "<<h2<<":"<<m1;getch();}

OUTPUT:

Program No.20

Write a program that inputs the salary of an employee from the user. It deducts the income tax from the salary on the following basis:

20% income tax if the salary is above Rs.30000. 15% income tax if the salary is between Rs.20000 and Rs.30000. 10% income tax if the salary is below Rs.20000.The program finally displays salary, income tax and the net salary.

#include<iostream.h>#include<conio.h>void main(){clrscr();unsigned int salary,tax,net;cout<<"Enter your salary = ";cin>>salary;if(salary>=30000){tax=(salary/100)*20;cout<<"Your Income Tax is "<<tax<<endl;

Page 20: Programming Lab Rep

}else if(salary>=20000){tax=(salary/100)*15;cout<<"Your Income Tax is "<<tax<<endl;}else if(salary<20000){tax=(salary/100)*10;cout<<"Your Income Tax is "<<tax<<endl;}net=salary-tax;cout<<"Your Net salary is "<<net;getch();}

OUTPUT:

Program No.21Write a program that displays the following menu for a parking area:

M =Motorcycle C =Car B =Bus

The program inputs the type of vehicle and number of days to park the vehicle. If finally displays the total charges for the parking according to the following: Motorcycle Rs.10 per day Car Rs.20 per day Bus Rs.30 per day

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

Page 21: Programming Lab Rep

clrscr();char n;int days,charges;cout<<"1. M\t=Motorcycle\n2. C\t=Car\n3. B\t=Bus"<<endl;cout<<"Enter Type of vehicle = ";cin>>n;cout<<"Enter days in which you park your vehicle in parking area = ";cin>>days;if(n=='M'||n=='m')

charges=days*10;else if(n=='C'||n=='c')

charges=days*20;else if(n=='B'||n=='b')

charges=days*30;cout<<"Your parking charges are "<<charges;getch();}

OUTPUT: