• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
August 13, 2024 |5.2K Views

Count the Digits in a Number | C Program

  Share   Like
Description
Discussion

In this video, we will see a C program to count digits in an integer

Here's the approach that we can follow:

Step 1: Consider a number N.
Step 2: Remove the last digit of the number by dividing it with 10.
Step 3: Increase the count of digits by 1.
Step 4: Repeat steps 2 and 3 untill the value of N becomes 0.

Here we see iterative method to calculate count digits in an integer:
1. Using while loop: The integer entered by the user is stored in the variable n. Then the while loop is iterated until the test expression n != 0 is evaluated to 0 (false).

For Examples:
Input: 6785
Output: 4

Input: 787646678
Output: 9

C Program to count the digits of a number:
https://www.geeksforgeeks.org/c-program-to-count-the-digits-of-a-number/