Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » C programs » C scanf() programs

Input float value and print it with specified digit after decimal point in C



Here, we are going to learn how to input a float value and print the value with specified given number of digits after the decimal point in C programming language?
Submitted by IncludeHelp, on September 11, 2018

Input a float value and we have to print the input value by specifying/setting different decimal precision in C.

Example:

    Input:
    Enter float value: 12.34567

    Output:
    12
    12.3
    12.35
    12.3457

To set decimal precision, we use the following format specifier with the printf() statement,

  • %.0f : No digit after decimal point
  • %.2f : 2 digits after decimal point
  • %.4f : 4 digits after decimal point

Program:

# include <stdio.h>

int main ()
{
	float value;	

	printf("Enter float value: ");
	scanf("%f", &value);

	//print without decimal point 
	printf("%0.0f\n", value);
	//print 1 digits after decimal point
	printf("%0.1f\n", value) ;
	//print 2 digits after decimal point 
	printf("%0.2f\n", value) ;
	//print 4 digits after decimal point 
	printf("%0.4f\n", value);

	return 0;
}

Output

Enter float value: 1.234567
1
1.2
1.23
1.2346





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates



© https://www.includehelp.com (2015-2018), Some rights reserved.