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
numberto store user input andcountto track the number of digits. - The user is prompted to enter a number using
scanf(). - The
whileloop runs as long asnumberis not zero. - Inside the loop, we remove the last digit using
number = number / 10. - Each time a digit is removed,
countis incremented. - When
numberbecomes zero, the loop stops, andcountholds 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
numberfor user input andcountto store the number of digits. - The user enters a number using
scanf(). - The
forloop initializescountto zero and runs whilenumberis not zero. - Inside the loop,
numberis divided by 10 to remove the last digit. - The
countvariable is incremented after each iteration. - Once
numberbecomes zero, the loop exits, andcountholds 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
numberfor user input andcountto track the number of digits. - The user enters a number using
scanf(). - We use
abs()fromstdlib.hto convert negative numbers to positive. - The loop removes digits using
number / 10and incrementscount. - Once
numberbecomes zero,countholds 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:
whileloop: Simple and efficient for counting digits.forloop: Alternative method with a similar approach.- Handling negative numbers: Using
abs()to process negative input.
