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

</>
Copy
#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:

  1. We declare an integer variable number to store user input and count to track the number of digits.
  2. The user is prompted to enter a number using scanf().
  3. The while loop runs as long as number is not zero.
  4. Inside the loop, we remove the last digit using number = number / 10.
  5. Each time a digit is removed, count is incremented.
  6. When number becomes zero, the loop stops, and count holds the total number of digits.
  7. 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

</>
Copy
#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:

  1. We declare number for user input and count to store the number of digits.
  2. The user enters a number using scanf().
  3. The for loop initializes count to zero and runs while number is not zero.
  4. Inside the loop, number is divided by 10 to remove the last digit.
  5. The count variable is incremented after each iteration.
  6. Once number becomes zero, the loop exits, and count holds the total number of digits.
  7. 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

</>
Copy
#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:

  1. We declare number for user input and count to track the number of digits.
  2. The user enters a number using scanf().
  3. We use abs() from stdlib.h to convert negative numbers to positive.
  4. The loop removes digits using number / 10 and increments count.
  5. Once number becomes zero, count holds the digit count.
  6. 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:

  1. while loop: Simple and efficient for counting digits.
  2. for loop: Alternative method with a similar approach.
  3. Handling negative numbers: Using abs() to process negative input.