Chapter 2


QUESTION NO 4

Repeat Exercise 3 by declaringnum1,num2,andnum3,andaverageof type double.Store75.35intonum1,-35.56intonum2,and15.76intonum3.

SOURCE CODE

#include "<"iostream">"
using namespace std;
main () {
int num1,num2,num3;
double averg;
num1=75.35,num2=-35.56,num3=15.76; averg=(num1+num2+num3)/3;
cout <<"average of all three digit is ="<< averg << endl;
cout <<"\n........................"<< endl;
system ("pause");
}

QUESTION NO 5

Consider the following C++ program in which the statements are in the incorrect order. Rearrange the statements so that it prompts the user to input the length and width of a rectangle and output the area and perimeter of the rectangle.

SOURCE CODE

#include "<"iostream">"
using namespace std;
int main ()
{
int length;
int width;
int area;
int perimeter;
cout << "Enter the length: ";
cout << endl;
cin >> length;
cout << "Enter the width: " ;
cout << endl;
cin>> width;
area = length * width;
cout << "Area = " << area << endl;
cout << "Perimeter = " << perimeter << endl;
system ("pause");
return 0;
}

QUESTION NO 6

Consider the following program segment: //include statement(s) //using namespace statement intmain() { //variable declaration //executable statements //return statement } 110 | Chapter 2: Basic Elements of C++ a. Write C++ statements that include the header filesiostream and string. b. Write a C++ statement that allows you to usecin, cout, and endl without the prefixstd::. c. Write C++ statements that declare the following variables:nameof type stringandstudyHoursof typedouble. d. Write C++ statements that prompt and input a string intonameand a doublevalue intostudyHours. e. Write a C++ statement that outputs the values ofnameandstudyHours with the appropriate text. For example, if the value of name is"Donald" and the value ofstudyHoursis 4.5, the output is: Hello,Donald!onSaturday,youneedtostudy4.5hoursfortheexam. f. Compile and run your program.

SOURCE CODE

#include 
using namespace std;
main () {
     double studeyHours;
     string name;
     studeyHours=4.5;
     cout << "hello ,anwaar! in holidays, you need to stduy  " << name;
cout <<" " << 4.5 << " hours for the exam." << endl; system ("pause"); }

QUESTION NO 7

Write a program that prompts the user to input a decimal number and outputs the number rounded to the nearest integer.

SOURCE CODE

#include
using namespace std;
double a;
int b;
main (){
      cout<<"Type Your no. here to round about it:";
      cin>>a;
      b=a+0.5;
      cout <

QUESTION NO 8

Consider the following program segment: //include statement(s) //using namespace statement intmain() { //variable declaration //executable statements //return statement } a. Write C++ statements that include the header filesiostream and string. b. Write a C++ statement that allows you to usecin, cout, and endl without the prefixstd::. c. Write C++ statements that declare and initialize the following named constants: SECRET of typeintinitialized to 11 and RATE of type doubleinitialized to 12.50. d. Write C++ statements that declare the following variables:num1,num2, andnewNumof typeint; nameof typestring; andhoursWorkedand wagesof typedouble. e. Write C++ statements that prompt the user to input two integers and store the first number in num1and the second number innum2. 2 Programming Exercises | 111 f. Write a C++ statement(s) that outputs the values ofnum1andnum2, indicating which is num1and which isnum2. For example, if num1is 8 andnum2is 5, then the output is: Thevalueofnum1=8andthevalueofnum2=5. g. Write a C++ statement that multiplies the value ofnum1by2, adds the value ofnum2to it, and then stores the result innewNum. Then, write a C++ statement that outputs the value ofnewNum. h. Write a C++ statement that updates the value ofnewNumby adding the value of the named constant SECRET. Then, write a C++ statement that outputs the value of newNum with an appropriate message. i. Write C++ statements that prompt the user to enter a person’s last name and then store the last name into the variablename. j. Write C++ statements that prompt the user to enter a decimal number between0and70and then store the number entered intohoursWorked. k. Write a C++ statement that multiplies the value of the named constant RATEwith the value ofhoursWorkedand then stores the result into the variablewages. l. Write C++ statements that produce the following output: Name: //output the value of the variable name PayRate:$ //output the value of the variable rate HoursWorked: //output the value of the variable //hoursWorked Salary:$ //output the value of the variable wages For example, if the value ofnameis "Rainbow"andhoursWorkedis 45.50, then the output is: Name:Rainbow PayRate:$12.50 HoursWorked:45.50 Salary:$568.75 m. Write a C++ program that tests each of the C++ statements that you wrote in parts a through l. Place the statements at the appropriate place in the previous C++ program segment. Test run your program (twice) on the following input data: a. num1=13,num2=28;name="Jacobson";hoursWorked= 48.30. b. num1=32,num2=15;name="Crawford";hoursWorked= 58.45.

SOURCE CODE


#include
using namespace std ;
main()
{     int seceret,num1,num2,newnum;
string name;
      double rate,hoursworked,wages;
      seceret=11;
      rate=12.50;
      num1=8;
      num2=5;
      cout<<"num1 = "<>name;
      cout<<"enter a decimal value between 0-70"<>hoursworked;
      
      wages = rate*hoursworked;
      
      cout<

QUESTION NO 9

Write a program that prompts the user to enter five test scores and then prints the average test score. (Assume that the test scores are decimal numbers.)

SOURCE CODE

#include 
using namespace std;
main () {
     float tst1,tst2,tst3,tst4,tst5,avg;
     cout << "entre a score of test 1,test 2,test 3,test 4, and test 5 sepearated by space" << endl;
     cin >>tst1>>tst2>>tst3>>tst4>>tst5;
     avg=(tst1+tst2+tst3+tst4+tst5)/5;
     cout << " total score  average is :"<< avg << endl;
     system ("pause");
     }





QUESTION NO 10

Write a program that prompts the user to input five decimal numbers. The program should then add the five decimal numbers, convert the sum to the nearest integer, and print the result.

SOURCE CODE


#include
using namespace std;
float a,b,c,d,e,sum,inte;
main (){
      cout<<"Enter your 1st no.:";
      cin>>a;
      cout<<"Enter your 2nd no.:";
      cin>>b;
      cout<<"Enter your 3rd no.:";
      cin>>c;
      cout<<"Enter your 4th no.:";
      cin>>d;
      cout<<"Enter your 5th no.:";
      cin>>e;
      
      sum=a+b+c+d+e;
      cout <<"the sum of five numbers "<< sum << endl;
      inte=sum+0.5;
      
      cout<<"integers is:"<

QUESTION NO 11

Write a program that does the following: a. Prompts the user to input five decimal numbers. b. Prints the five decimal numbers. c. Converts each decimal number to the nearest integer. d. Adds the five integers. e. Prints the sum and average of the five integers.

SOURCE CODE

#include
using namespace std;
float a,b,c,d,e;
int f,g,h,i,j,avrg,sum;
main(){
      cout<<"Enter your 1st no.:";
      cin>>a;
      cout<<"Enter your 2nd no.:";
      cin>>b;
      cout<<"Enter your 3rd no.:";
      cin>>c;
      cout<<"Enter your 4th no.:";
      cin>>d;
      cout<<"Enter your 5th no.:";
      cin>>e;
      cout<<"1st no.    "<

QUESTION NO 12

Write a program that prompts the capacity, in gallons, of an automobile fuel tank and the miles per gallons the automobile can be driven. The program outputs the number of miles the automobile can be driven without refueling.

SOURCE CODE

#include
using namespace std;

int main()
{
	int h,m,s,numbers,remainder;
	cout<<"enter the seconds..:";
	cin>>numbers;
        
	h=numbers/3600;
	remainder=numbers%3600;
	m=remainder/60;
	s=remainder%60;
	cout<<"\t\t"<

QUESTION NO 13

Write a C++ program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. (For example, if the elapsed time is 9630 seconds, then the output is 2:40:30.)

SOURCE CODE


#include
using namespace std;

int main()
{
	int h,m,s,numbers,remainder;
	cout<<"enter the seconds..:";
	cin>>numbers;
	h=numbers/3600;
	remainder=numbers%3600;
	m=remainder/60;
	s=remainder%60;
	cout<<"\t\t"<

QUESTION NO 14

Write a C++ program that prompts the user to input the elapsed time for an event in hours, minutes, and seconds. The program then outputs the elapsed time in seconds.

SOURCE CODE


#include
using namespace std;
int input;
int a;
int mint,hour,sec;
main (){
      cout<<"    Please enter your seconds to convert them into following format\n";
      cout<<"             Hours        -          Minutes      -    Seconds\n\n";
      cout<<"             ";
      cin>>input;
      hour=input/3600;
      a=input%3600;    
      mint=a/60;
      sec=a%60;
      
      cout<<"    OKAY !\n\n    Your input seconds have changed into following format:\n\n"<

No comments:

Post a Comment