196k views
4 votes
Write a program that uses the conversion specifier g to output the value 9876.12345. Print the value with precisions ranging from 1 to 9.

User Pallupz
by
5.3k points

1 Answer

4 votes

Answer:

Written in C++

#include <stdio.h>

int main(){

double value = 9876.12345;

printf("1: %.1g\\", value);

printf("2: %.2g\\", value);

printf("3: %.3g\\", value);

printf("4: %.4g\\", value);

printf("5: %.5g\\", value);

printf("6: %.6g\\", value);

printf("7: %.7g\\", value);

printf("8: %.8g\\", value);

printf("9: %.9g\\", value);

return 0;

}

Step-by-step explanation:

This declares and initializes value as double

double value = 9876.12345;

This prints the first precision

printf("1: %.1g\\", value);

This prints the second

printf("2: %.2g\\", value);

This prints the third

printf("3: %.3g\\", value);

This prints the fourth

printf("4: %.4g\\", value);

This prints the fifth

printf("5: %.5g\\", value);

This prints the sixth

printf("6: %.6g\\", value);

This prints the seventh

printf("7: %.7g\\", value);

This prints the eight

printf("8: %.8g\\", value);

This prints the ninth

printf("9: %.9g\\", value);

The precision is printed using the following syntax: printf("%.ag\\", value);

Where a represents the number of digits to print

User Romal
by
5.4k points