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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



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 some rights reserved.