Count Number of Digits in Number program in C

program-to-count-number-of-digits-in-c-language-using-loops

Program Description:

Write a program to Count the number of digits in C language using loops (while and for, etc). The program should accept a positive number from the user and count the number of individual digits in the given number. Also, display the error message on invalid input.

Here is an example of inputs and outputs.

Expected Inputs and Outputs:

Example 1: Input:

Enter a positive number : 76532

Output:

Number of Digits in 76532 number are: 5

Example 2: Input:

Enter a positive number : 2

Output:

Number of Digits in 2 number are: 1

Pre Requisites:

It is recommended to have Knowledge of C Loops and Arithmetic operators. Please go through the following articles.

Program to Count the number of digits in C Algorithm:

  • Take the Input from the user and store it in variable num.
  • If the user enters a positive number then iterate over the number using the loop
  • Take the backup of the num to tempThis step is optional, You can directly use the num as well.
  • We will iterate over the number using the division operator. I.e temp = temp/10,
    • We can remove the last digit from the number by dividing the given number by 10.
      • For example:  123 / 10  = 12
  • At each iteration, Increment the counter ( digitCnt)
  • The loop will run until the number is Greater-than 0 ( temp > 0 )
  • The counter ( digitCnt) will have the total number of digits in the number ( num or temp)
  • If the user enters invalid input like a negative number. Display the error message – ERROR: Please enter valid number, Try Again.
    • Allow the user to enter the number again using the Goto Statement, which can be used to jump from one statement in the function to another statement.

Program Count number of digits in C language using for loop:

Output:

Count-Number-Digits-in-a-Number-in-c-language-result

Compile and run the program.

If the user enters a Negative number.

As you can see from the above output, If the user enters a Negative number, The program asked for the number again. ( because of the goto INPUT; and INPUT goto statement and labels.)

Count the Number of Digits in a Number Program in C using for loop:

Here is the program with the for loop. The Program logic remains the same as the above while loop example, but as it is for loop, We can use the for loops Init and Update steps to update our number ( temp).

Program Output:

Let’s test the negative number test case as well.

Practice Program:

  • Try to rewrite the above program using the do while loop and goto statement.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

5 Responses

  1. […] Program to count number of digits of a Number in C […]

  2. […] C Program to Count the number of Digits of a Number […]

  3. […] C Program to Count the number of Digits of a Number […]

  4. […] need to Calculate the Number of digits in the number. Which is used to calculate the power. We used a function countDigits and passed the input number […]

  5. […] C Program to Count the number of Digits of a Number […]

Leave a Reply