Count the Digits in a Number using Loops in C
To count the number of digits in a given integer in C, we can use loops such as while
or for
. The process involves repeatedly dividing the number by 10 and counting the number of iterations until the number becomes zero. In this tutorial, we will explore different methods to count digits using loops with detailed explanations and examples.
Examples to Count Digits in a Number
1. Counting Digits Using a while
Loop
In this example, we will use a while
loop to count the number of digits in a positive integer.
main.c
#include <stdio.h>
int main() {
int number, count = 0;
// Input from the user
printf("Enter a number: ");
scanf("%d", &number);
// Loop to count digits
while (number != 0) {
number = number / 10; // Remove last digit
count++; // Increase count
}
printf("Number of digits: %d\n", count);
return 0;
}
Explanation:
- We declare an integer variable
number
to store user input andcount
to track the number of digits. - The user is prompted to enter a number using
scanf()
. - The
while
loop runs as long asnumber
is not zero. - Inside the loop, we remove the last digit using
number = number / 10
. - Each time a digit is removed,
count
is incremented. - When
number
becomes zero, the loop stops, andcount
holds the total number of digits. - The final digit count is printed using
printf()
.
Output:
Enter a number: 12345
Number of digits: 5
2. Counting Digits Using a for
Loop
In this example, we will achieve the same result using a for
loop instead of a while
loop.
main.c
#include <stdio.h>
int main() {
int number, count;
// Input from the user
printf("Enter a number: ");
scanf("%d", &number);
// Initialize count
for (count = 0; number != 0; count++) {
number = number / 10; // Remove last digit
}
printf("Number of digits: %d\n", count);
return 0;
}
Explanation:
- We declare
number
for user input andcount
to store the number of digits. - The user enters a number using
scanf()
. - The
for
loop initializescount
to zero and runs whilenumber
is not zero. - Inside the loop,
number
is divided by 10 to remove the last digit. - The
count
variable is incremented after each iteration. - Once
number
becomes zero, the loop exits, andcount
holds the total number of digits. - The final count is displayed using
printf()
.
Output:
Enter a number: 987654
Number of digits: 6
3. Counting Digits of a Negative Number
In this example, we handle cases where the input number is negative by converting it to positive before counting the digits.
This program works for both positive and negative numbers.
main.c
#include <stdio.h>
#include <stdlib.h> // For abs() function
int main() {
int number, count = 0;
// Input from the user
printf("Enter a number: ");
scanf("%d", &number);
// Convert negative number to positive
number = abs(number);
// Loop to count digits
while (number != 0) {
number = number / 10;
count++;
}
printf("Number of digits: %d\n", count);
return 0;
}
Explanation:
- We declare
number
for user input andcount
to track the number of digits. - The user enters a number using
scanf()
. - We use
abs()
fromstdlib.h
to convert negative numbers to positive. - The loop removes digits using
number / 10
and incrementscount
. - Once
number
becomes zero,count
holds the digit count. - The total number of digits is displayed using
printf()
.
Output:
Enter a number: -2468
Number of digits: 4
Conclusion
In this tutorial, we learned different ways to count the digits in a number using loops:
while
loop: Simple and efficient for counting digits.for
loop: Alternative method with a similar approach.- Handling negative numbers: Using
abs()
to process negative input.