C Program to print the given fractional number in 2 digit decimal format

Get a fractional number from user and display the same in two digit precision

Sample Input 1:

56.3426

Sample Output 1:

56.34

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#include<stdio.h> int main() { //write your code here }

Program or Solution

				
			
					
//Program to print the given float (fractional) number in 2digit decimal format
#include<stdio.h>
int main()
{
	float num;
	printf("Enter a fractional number:");
	scanf("%f",&num); 	
	printf("\nEntered number in 2digit precision: %.2f",num);	
	return  0;
}
			
				
			

Program Explanation

scanf is a function available(predefined) in c library to get input from user via keyboard and stores the same in a variable.

Here it reads input number and stores it in a variable num.

Format Specifier "%f" reads input as floating number.

(fractional number) printf is a function available(pre defined) in C library which is used to print the specified content in Monitor.

Here it prints the value of the variable num.

Format Specifier "%2f" prints value as Floating Point Number with 2digit Precision(.00).

Comments