Think of a number and desire that it must always have a particular number of digits showing. This program works like a help that makes sure your number looks as it is. Just tell it your number and how many digits long you want it to be. If your number is short, the program just enters leading zeros until it straightens it out. This is nice when you want a whole row of numbers to line up perfectly, like with a game score or time display.
C Program to input and print an integer with zero padding.
#include <stdio.h>
int main() {
int numberFromUser; // This will hold the number you type.
int desiredLength; // This will hold how many digits you want in total.
// First, the program will ask you to enter a whole number.
printf("Please type in a whole number and press Enter: ");
scanf("%d", &numberFromUser);
// Next, the program will ask you for the total length you want the number to have.
// For example, if you type 4, and your number is 12, it will be shown as 0012.
printf("Now, tell me the total number of digits you want the number to have (e.g., 4): ");
scanf("%d", &desiredLength);
// Finally, the program will print your number, adding zeros at the front if needed
// to reach the desired length. The '%0*d' part is what does this:
// - '%' signals a special formatting instruction.
// - '0' tells the program to fill any extra space with zeros.
// - '*' is a placeholder that gets replaced by the 'desiredLength' you entered.
// - 'd' indicates that we are printing a whole number (an integer).
printf("Here's your number with leading zeros: %0*d\n", desiredLength, numberFromUser);
return 0; // This means the program finished successfully.
}
Program Description:
Step by Step Execution with Description and Output:
Step 1: The Program Starts
The program comes alive. By now, nothing appears on the screen.
Step 2: Asking for Input
printf("Please type in a whole number and press Enter: ");
causes the above text to appear on your screen:
Please type in a whole number and press Enter:
The program is now waiting for you to type a number.
Step 3: User Input
You enter the number 12 and press Enter.
Step 4: Reading the Input
scanf("%d", &numberFromUser);
takes the command 12 that you entered and saves it into the memory of the program.
Step 5: Asking for Desired Length
printf("Now, tell me the total number of digits you want the number to have (e.g., 4): ");
displays the next text on your screen:
Now, tell me the total number of digits you want the number to have (e.g., 4):
The program is now waiting for your next input.
Step 6: User Input for Length
You type in the number 5 and press Enter.
Step 7: Reading Desired Length
The line:
scanf("%d", &desiredLength);
takes the 5 you typed and stores it in the program's memory.
Step 8: Formatting and Printing the Output
printf("Here's your number with leading zeros: %0*d\n", desiredLength, numberFromUser);
does like this:
"Here's your number with leading zeros: " - This text is being printed on the screen.
%0*d - This is the formatting instruction. It tells the program to take the numberFromUser (which is 12) and print it as a whole number (d). The 0 means to add leading zeros if needed, and the * means the total width should be taken from the desiredLength (which is 5).
\n - This adds a newline at the end, moving the cursor to the next line.
Step 9: Output on Your Screen
According to the format, the number 12 will be forwarded with three leading zeros, hence making a total of five digits. The complete output that you will see is:
Here's your number with leading zeros: 00012
Step 10: Ending of Program
return 0;
indicates the end of execution of the program without any errors. The program closes, and you return to your terminal or command prompt.
Basic and Operators practice program assignments in C Language :
Practice/Assignment set 1:
1.Write a program in C to Input integer, float and character values using one scanf().
2.Write a program in C language to find the Area of Circle.
3.Write a program in C language to Input an integer value and print with padding by Zeros in C
4. Write a program in C language to Input float value and print it with specified digit after decimal point in C
5.Write a program in C language to find sum of the digits of any three digit number.
6. Write a program in C language to Input an unsigned integer value using scanf().
7.Write a program in C language to Find area of Triangle
8.Write a program in C language to Input a hexadecimal value using scanf() in C
9.Write a program in C language to Input octal value using scanf()
10.Write a program in C language to Convert temperature given in farenheight to degree celcius.
11.Write a program in C language to Input octal value using scanf()
12.Write a program in C language to Input decimal, octal and hexadecimal values in character variables using scanf() in C
13.Write a program in C language to Input an integer value in any format (decimal, octal or hexadecimal) using '%i' in C
14.Write a program in C language to Input side value and find area of Square.
15.Write a program in C language to Input individual characters using scanf() in C
16.Write a program in C language to Read a memory address using scanf() and print its value in C
17.Write a program in C language to Skip characters while reading integers using scanf() in C
18.Print your name, date of birth, and mobile number
19. Write a C program to convert specified days into years, weeks and days.
Note: Ignore leap year.
21.Write a C program that accepts two item's weight and number of purchases (floating point values) and calculates their average value.
22.Write a C program that accepts an employee's ID, total worked hours in a month and the amount he received per hour. Print the ID and salary (with two decimal places) of the employee for a particular month.
Test Data :
Input the Employees ID(Max. 10 chars): 0342
Input the working hrs: 8
Salary amount/hr: 15000
Expected Output:
Employees ID = 0342
Salary = U$ 120000.00
23.Write a C program that accept an students roll number and six subject markes, find total and average marks.
24.Write a C program that accepts an employee's ID, basic salary in a month and calculate total salary.
The formulat for
Total salary=(basic salary-tax)+House Rent allowance+Bonus+Traveling allowance+other allowance
tax 10% of basic salary
House Rent allowance 17% of basic salary
Bonus 7% of basic salary
Traveling allowance 8% of basic salary
other allowance 10% of basic salary
25.Write a C program to convert a given integer (in days) to years, months and days, assuming that all months have 30 days and all years have 365 days.
Test Data :
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s)
Practice/Assignment set 2:
1. C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2
2. C program to find sum of the all natural numbers from 1 to N.
Series: 1+2+3+4+..N
3.C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
4) C program to find sum of following series:
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
5) C program to find sum of following series:
1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms