Here, in this tutorial, you will learn how to write a C program to count the digits of a given number using a while loop. We will see various examples, logic, algorithms, and program explanations for your better understanding of this program.
Required Knowledge
- C Programming Basics
- C Programming Operators
- C printf and Scanf
- While loop in C
Problem statement
You have to Write a C program that takes a number as input from the user. After taking the input you have to count the number of digits in the input number using a while loop. Program should provide a solution to find the count of digits in a number using a while loop in the C programming language.
For a better understanding, below we have an example that help you to understand what exactly we wanted to achieve in this program.
Example
Input:
Enter a number: 12345
Output:
The number of digits in 12345 is: 5
In the above example, you can see that the user has given input 12345. As the input number has 5 digits so output will print as 5.
Algorithm
- Start the program.
- Declare the variables:
num
to store the input number,count
to store the count of digits, andtemp
as a temporary variable to hold the value ofnum
. - Read the value of
num
from the user. - Initialize
count
to 0. - Assign the value of
num
totemp
. - Enter the while loop with the condition
temp != 0
.- Divide
temp
by 10 to remove the last digit. - Increment
count
by 1.
- Divide
- Exit the while loop.
- Print the count of digits as: “The number of digits in num is: count”.
- End the program.
Logic used to solve this problem
- Declare a variable to hold the input number and another variable to store the count of digits.
- Prompt the user to enter a number.
- Read and store the entered number in the variable.
- Initialize the count variable to 0.
- Use a while loop that runs as long as the number is greater than 0.
- Inside the loop, increment the count variable by 1.
- Reduce the number by removing the last digit using the integer division (/) operator.
- Continue the loop until the number becomes 0.
- Print the count of digits.
- Finish the program.
Program to count the digits of a given number
#include <stdio.h>
int main() {
int num, count = 0, temp;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (temp != 0) {
temp /= 10;
count++;
}
printf("The number of digits in %d is: %d\n", num, count);
return 0;
}
Output
Enter a number: 10
The number of digits in 10 is: 2
Program Explanation
- The program takes input for the number whose digits are to be counted.
- It initializes a variable
count
to 0, which represents the count of digits. - The program enters a while loop with the condition
number > 0
to extract the digits of the given number. - Inside the while loop, it calculates the remainder of the number divided by 10.
- It increments the value of
count
by 1 to indicate the presence of another digit. - It updates the value of the number by dividing it by 10, effectively removing the rightmost digit.
- The loop continues until the number becomes 0, indicating that all digits have been processed.
- Once the while loop is complete, the program prints the value of
count
, which represents the number of digits in the given number. - The program ends.
Conclusion
The C program successfully counts the number of digits in a given number using a while loop. By dividing the number by 10 repeatedly until it becomes 0, it iterates through the digits and increments a counter variable. The program then displays the count of digits on the screen.