Coupon Accepted Successfully!


More Programs

3.51 Write a program to calculate the simple interest and total amount, input principal amount, period, and rate of interest.

Explanation: In the above program, integer variables p_amount, period, i_rate, interest, and t_amount are declared. The principal amount, period in years, and rate of interest are read through the keyboard using respective variable with cin statement.


The interest is calculated by the statement interest=(p_amount*period*i_rate)/100 and the interest calculated is assigned to variable interest. The total amount is calculated by adding interest in the principle amount.


3.52 Write a program to set width and display the integer number.

Explanation: In the above program, the cout.width(7) statement sets width to 7. The cout statement displays the five digits. The total number of digits are less than the total width. Hence, two blank spaces are displayed at the beginning as shown below.















3.53 Write a program to demonstrate use of showpos and showpoint flags.

Explanation: The output of the program can be understood from the following table.











The showpos flag displays the positive sign at the beginning. The showpoint option displays decimal point and trailing zeros. The internal and adjustfield flags add blank spaces between the sign and the number.

3.54 Write a program to display results in right justification.

Explanation: In the above program, the integer variable k is initialized to 0 and long double variables n and f are initialized to 1.00. The precision is set to 0. The setf() function sets the precision to fixed position. The statement cout.width (15) fixes the field width. Thus, using while loop repetitive operations are performed. The output of the program is given above.

3.55 Write a program to display square and cube of numbers from 1.5 to 16.5 in table format.

Explanation: In the above program the variable num is initialized to 1.5. The while loop is executed repetitively and square and cube of the number are calculated using the pow() function. The functions cout.width() and cout.precision() set the field width and the number of floating points. The output of the program is shown above.

3.56 Write a program to display octal and hexadecimal equivalents of decimal numbers 100 to 200 with a difference of 10.

Explanation: In the above program, ios base fields such as oct, hex, and dec are set using setf() function. After this setting the decimal number given in cout() before printing is converted to the above formats. The while loop repetitively performs this task and the result is displayed as above.

3.57 Write a program to create manipulator. Use setf() function.

Explanation: In the above program, rs and arg are two user-defined manipulators. The arg manipulator contains various member functions of ios class that are used to manage the screen.

3.58 Write a program to define macro for %d and <<. Use them in cout and printf statements.

Explanation: The operator << and variable j together are assigned to macro d and the format string %d is assigned to macro i. Both are used in cout and printf statements, respectively.

3.59 Write a program to display trailing decimal zeros using formatted functions.

Explanation: By default, the cout statement omits the trailing decimal zeros. The trailing zeros can be displayed using a setting as shown in the above program. The result of the above program is shown as follows in tabular format for the sake of understanding.


3.60 Write a program to display contents of an array using width() function. Fill the blank space with ‘*’.

Explanation: In the above program, an array item[3][3] is declared and initialized with float numbers. The following formatting statements are used.


cout.precision(2) – Sets decimal point limit to two digits.

cout.fill(‘*’) – Fills ‘*’ in blank space of field.

cout.width(8) – Sets field width to 8.

The first and second for loops are used to read and display the contents of the array on the screen. The output of the program is as follows.


Here, each field has width 8. The numbers are displayed with right justification. The blank spaces are filled with ‘*’ symbol.

Test Your Skills Now!
Take a Quiz now
Reviewer Name