Programming Exercise Solutions Chapter 4 1. #include <iostream.h> #include <conio.
h> void main() { int marks = 786; float avg = 89.75; char grade = 'A'; clrscr(); cout<<"I am Usman Khalil:<<endl; cout<<"My total marks are:<<marks<<endl; cout<<"My average is: "<< avg<<endl; cout<<"My grade is:<< grade; getch(); } 2. #include <iostream.h> #include<iomanip.h> #include <conio.h> void main() { float r, area, cir; clrscr(); cout<<"Enter radius: "; cin>>r; area = 4.0 * 3.14 * r * r; cir = (4.0/3.0) * 3.14 * r * r * r; cout<<"Area is:"<<setprecision(2)<< area<<endl; cout<<"Circumference is:"<<setprecision(2)<<cir; getch(); } 3. #include <iostream.h> #include <math.h> #include<conio.h> void main() { float a, b, c; double s, area; clrscr(); cout<<"Enter side A: "; cin>>a; cout<<"Enter side B: "; cin>>b; cout<<"Enter side C: "; cin>>c; s = (a + b + c) / 2.0; area = sqrt(s * (s - a) * (s - b) * (s - c)); cout<<"Area of triangle is:"<<area;
Chapter 4 Input and Output (Exercise Solutions)
getch(); } 4. #include <iostream.h> #include <conio.h> void main() { float kilo; int miles; clrscr(); cout<<"Enter miles: "; cin>>miles; kilo = 1.609 * miles; cout<<miles<< miles:<<kilo<< kilometers; getch(); } 5. #include <iostream.h> #include <conio.h> void main() { int a, b, c, d, sum, product; float avg; clrscr(); cout<<"Enter 4 numbers:"; cin>>a>>b>>c>>d; sum = a + b + c + d; product = a * b * c * d; avg = sum / 4.0; cout<<"Sum of all numbers: << sum<<endl; cout<<"Product of all numbers:<<product<<endl; cout<<"Average of all numbers:<<avg<<endl;; getch(); } 6. #include <iostream.h> #include <conio.h> void main() { int age, months, days; clrscr(); cout<<"Enter age in years:"; cin>>age); months = age * 12; days = age * 365; cout<<"Age in months:"<<months<<endl; cout<<"Age in days:<<days; getch(); } 7. #include <iostream.h> #include <conio.h> void main()
IT Series Object-Oriented Programming using C++
{ int n, s, c; clrscr(); cout<<"Enter a number:"; cin>>n; s = n * n; c = n * n * n; cout<<"Square is: << s<<endl; cout<<"Cube is:<< c<<endl; getch(); } 8. #include <iostream.h> #include <conio.h> void main() { int pages, pagesDaily, days, remaining, completed; clrscr(); cout<<"Enter total pages of book: "; cin>>pages; cout<<"How many pages you read daily? "; cin>>pagesDaily; cout<<"How many days you read the book? "; cin>>days; completed = days * pagesDaily; remaining = pages completed; cout<<"You have read<<completed<<pages<<endl; cout<<"Remaining pages are<< remaining; getch(); } 9. #include <iostream.h> #include <conio.h> void main() { float distance, liters; clrscr(); cout<<"Enter petrol in liters: "; cin>>liters; distance = liters * 5.3; cout<<"Car can cover <<distance<< miles in <<liters<< liters."; getch(); } 10. #include <iostream.h> #include <conio.h> void main() { int fee, students; long total; clrscr(); cout<<"Enter total students: ";
Chapter 4 Input and Output (Exercise Solutions)
cin>>students; cout<<"Enter fee per student: "; cin>>fee; total = students * fee; cout<<"Total fee = << total; getch(); } 11 #include <iostream.h> #include <conio.h> void main() { float cel, faren; clrscr(); cout<<"Enter temperature in farenheit: "; cin>>faren; cel = 5.0 / 9.0 * ( faren 32); cout<<"Temperature in celcius is<<cel; getch(); } 12. #include <iostream.h> #include <conio.h> void main() { int n, a, b, c; clrscr(); cout<<"Enter a 3-digit number: "; cin>>n; a = n / 100; n = n % 100; b = n / 10; c = n % 10; cout<< a<<endl; cout<< b<<endl; cout<<c<<endl; getch(); } 13. #include <iostream.h> #include <conio.h> void main() { clrscr(); cout<<"1 2 3 4 5 \n6 7 8 9 10"; getch(); }