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?
By IncludeHelp Last updated : March 10, 2024
Problem statement
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
Print with padding by Zeros in C
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
C program to input an integer value and print with padding by Zeros
# 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
C scanf() Programs »