A 114 : Printing Floating Point Integers HackerRank Answer

Did you notice the %.2f used in the printf statement used to print floating point numbers?

The .2 stands for printing two digits after the decimal point.

Hence 2 will be printed as 2.00, 1.666666.. will be rounded off and printed as 1.67 and so on.

For this task, write a code that takes as input a floating point number and prints it upto two, four and six decimal places, each on a new line.

Sample Input 0

21.6666

Sample Output 0

21.67
21.6666
21.666600
Source Code
#include <stdio.h>

int main()
{
	double var;
	//input the variable var and print it upto 2, 4 and 6 decimal places here.
	//note that every output must be on a new line
	//you can use the '\n' token to go to a new line
scanf("%lf", &var);
printf("%.2lf\n%.4lf\n%.6lf",var,var,var);



	return 0;
}

Comments