C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Loop Programs in C

Count digits in a number

Introduction

Counting the digits of a number means finding how many numerical digits it contains.

Example:

  • Input: 12345 → Output: 5
  • Input: -9876 → Output: 4
  • Input: 0 → Output: 1

We’ll use a loop to repeatedly divide the number by 10 until it becomes 0.

C Program: Count digits in a number

NOTE: The Program is written using for, while and do..while loop.
Method 1 is for loop, Method 2 is while loop and Method 3 is do..while loop

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int num, original, count = 0;

 

    // Input

    printf("Enter an integer: ");

    scanf("%d", &num);

 

    original = num; // store original number

 

    // Handle 0 as a special case

    if (num == 0) {

        count = 1;

    } else {

        // Make number positive if negative

        if (num < 0) {

            num = -num;

        }

 

        // Count digits using for loop

        for (; num != 0; num /= 10) {

            count++;

        }

    }

 

    // Display result

    printf("Number of digits in %d = %d\n", original, count);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter an integer: 12345
Number of digits in 12345 = 5

OUTPUT 2 :
Enter an integer: -9876
Number of digits in -9876 = 4

OUTPUT 3 :
Enter an integer: 0
Number of digits in 0 = 1

Explanation

  1. Read the number num from the user and store it in original.
  2. If num is 0, set count = 1.
  3. If num is negative, convert it to positive.
  4. Use a for loop to divide num by 10 repeatedly, incrementing count each time.
  5. Print the count of digits.

 

C Program: Count digits in a number

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int num, original, count = 0;

 

    // Input

    printf("Enter an integer: ");

    scanf("%d", &num);

 

    original = num; // store original number

 

    // Handle 0 as a special case

    if (num == 0) {

        count = 1;

    } else {

        // Make number positive if negative

        if (num < 0) {

            num = -num;

        }

 

        // Count digits

        while (num != 0) {

            num /= 10; // remove last digit

            count++;

        }

    }

 

    // Display result

    printf("Number of digits in %d = %d\n", original, count);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter an integer: 12345
Number of digits in 12345 = 5

OUTPUT 2 :
Enter an integer: -9876
Number of digits in -9876 = 4

OUTPUT 3 :
Enter an integer: 0
Number of digits in 0 = 1

Explanation

  1. Read the number num from the user and store it in original.
  2. If num is 0, set count = 1.
  3. If num is negative, convert it to positive.
  4. Use a while loop to divide num by 10 repeatedly, incrementing count each time.
  5. Print the count of digits.

 

C Program: Count digits in a number

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int num, original, count = 0;

 

    // Input

    printf("Enter an integer: ");

    scanf("%d", &num);

 

    original = num; // store original number

 

    // Make number positive if negative

    if (num < 0) {

        num = -num;

    }

 

    // Count digits using do-while loop

    if (num == 0) {

        count = 1;

    } else {

        do {

            num /= 10; // remove last digit

            count++;

        } while (num != 0);

    }

 

    // Display result

    printf("Number of digits in %d = %d\n", original, count);

 

    return 0;

}

Output

 
OUTPUT 1 :
Enter an integer: 12345
Number of digits in 12345 = 5

OUTPUT 2 :
Enter an integer: -9876
Number of digits in -9876 = 4

OUTPUT 3 :
Enter an integer: 0
Number of digits in 0 = 1

Explanation

  1. Read the number num from the user and store it in original.
  2. If num is 0, set count = 1.
  3. If num is negative, convert it to positive.
  4. Use a do..while loop to divide num by 10 repeatedly, incrementing count each time.
  5. Print the count of digits.