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 an integer value and print with padding by Zeros in C



Here, we are going to learn how to input an integer number and print it with padding by zeros in C programming language?
Submitted by IncludeHelp, on September 11, 2018

Input an integer value and we have to pad the value by Zeros in C.

Example:

    Input:
    Enter an integer value: 12 

    Output: 
    2 digit padding: 12
    3 digit padding: 012
    4 digit padding: 0012

To print with padding by Zeros, we use the following format specifier with the printf() statement,

  • %d : No padding
  • %01d : 1 digit padding with zeros
  • %02d : 2 digits padding with zeros
  • %03d : 4 digits padding with zeros

Program:

# include <stdio.h>

int main ()
{
	int value;

	printf ("Enter an integer value: ");
	scanf ("%d", &value);

	printf ("2 digits padding: %02d\n", value) ;
	printf ("3 digits padding: %03d\n", value) ;
	printf ("4 digits padding: %04d\n", value) ;

	return 0;
}

Output

Enter an integer value: 12
2 digits padding: 12
3 digits padding: 012
4 digits padding: 0012





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.