Chapter 6: Getting Started with C++ Programming Questions Solutions


Que.17. Write a program to display the following output using a single cout statement:
Program = 20
Documentation = 23
Logic = 21
Flow Chart = 18

// Program to display newline staements in single cout statement

#include < iostream.h>
  void main()
{
cout << "Program = 20 \n Documentation = 23 \n Logic = 21 \n Flow Chart = 18";
}

Que.18. Write a program to read values of w, x, y and z and display the value of P, where
              P = (w + x) / (y - z)

// Program to read and display calculated values
#include <iostream.h>
   int main()
{
 int w, x, y, z, P;
 cout << "Enter values for w,x,y and z in order:\n";
 cin >> w >> x >> y >> z;
 P = (w+x)/(y-z)
 cout << "Value of P = " << P;
 return 0;
}

Que.19. Write a C++ program that reads temperature in Celsius and displays it in Fahrenheit

// Program to read temperature in Celsius and display in Fahrenheit
#include <iostream.h>
  void main()
{
float c,f;
cout << "Enter temperature in Celsius:\n";
cin >> c;
// Formula for Fahrenheit = (9/5)*C + 32 = 1.8*c + 32
f = 1.8*c + 32;
cout << "\n Temperature in Fahrenheit is " << f;
}

Que.20. Write a program to display on screen
a) the character 'z'  b) the name 'Mohan' c) the number 1977
(i) Using single cout statement (ii) Using multiple cout statement

(i) Using single cout statement
 #include <iostream.h>
int main()
{
 cout << "z Mohan 1977";
}

(ii) Using multiple cout statement
#include <iostream.h>
int main()
{
 cout << "z";
 cout << "Mohan";
 cout << "1977" ;
}

Que.21. Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter the number of gallons and then display the equivalent cubic feet.

// Program to convert gallons into cubic feet
#include <iostream.h>
  void main()
{
 float g,c;
 cout << "Enter number of gallons:\n";
 cin >> g;
// 1 cubic feet = 7.481 gallons
// so 1 gallon = 1/7.481 cubic feet = 0.134 cubic feet
 c = g*0.134;
 cout << g << " has " << c << " cubic feet";
}

Que.22. Write a program to generate the following table:

1992                                                                17421
1993                                                                29210
1994                                                                100523
1995                                                                106902
1996                                                                127000

Use a single cout statement for output. (Hint: Make use of \n and \t.)

#include <iostream.h>
void main()
{
 cout << "1992 \t 17421 \n 1993 \t 29210 \n 1994 \t 100523 \n 1995 \t 106902 \n 1996 \t 127000";
}

Que.23.Write a program that generates the following output:

5
10
9

Assign value 5 to a variable using = Multiply it with 2 to generate 10 and subtract 1 to generate 9


#include <iostream.h>
int main()
{
 int x = 5; // Used a variable to initialize 5 to it
 int y = x*2; // Used another variable to multiply the previous one by 2
 int z = y-1; // Used a third variable to subtract 1 from it
 cout << x << "\n" << y << "\n" << z; // Display the output
return 0;
}

Que.24.Write a C++ program that accepts radius of a circle and prints its area.


#include <iostream.h>
 void main()
{
 int radius;
 cout << "Enter radius: ";
 cin >> radius;
 cout << Area= " << 3.14*radius*radius; // i.e., Pi-R-Square
}

Que.25.Write a program to print the output mentioned in question 23 in different lines i.e., all values must appear in the different lines.

#include <iostream.h>
int main()
{
  cout << "5" << endl;
  cout << "10" << endl;
  cout << "9" << endl; 

return 0;
}

Que.26.Write a C++ program that accepts marks in 5 subjects and outputs the average marks.

#include <iostream.h>
void main()
{
 int m1,m2,m3,m4,m5;
 int average;
 cout << "Enter the marks of 5 subjects in order:";
 cin >> m1 >> m2 >> m3 >> m4 >> m5;
 average = (m1+m2+m3+m4+m5)/5;
 cout << "\n Average Marks = " << average;
}


Que.27.Write a C++ program to accept two integers and print their sum.

#include <iostream.h>
void main()
{
 int x,y;
 cout << "Enter two integers:";
 cin >> x >> y;
 cout << "Sum of integers = " << x+y;
}

4 Reviews for "Chapter 6: Getting Started with C++ Programming Questions Solutions"