C Program to Count Number of Digits in an Integer

devquora
devquora

Posted On: Jan 27, 2023

C Program to Count Number of Digits in an Integer

 

Overview:

In this article, we will look at how to count the number of digits in an integer. This integer is nothing but the number entered by the user. We know that Software enables hardware to work. To do any task easily, We have to use software. Program is a subset of a software. Here, our specific task is to count the number of digits in an Integer. Now, First of all, I would like to explain the number, types of the number and Integer? After all, I will demonstrate the logic of C Program to Count Number of Digits in an Integer and explain the working of its internal execution with output.

Table of contents:

  1. What is a Number?
  2. What are the types of the Number?
  3. What is Integer?
  4. Logic to Count Number of Digits in an Integer
  5. Demonstration to Count Number of Digits in an Integer
  6. C Program to Count Number of Digits in an Integer
  7. Explanation of the Execution of the C program to Count Number of Digits in an Integer with output

What is a Number?

The Number is a combination of digits and decimal point. The repetition of digits does not matter but the decimal point does not repeat. For instance, a Number 1123.171 uses 4 digits i.e. 1,2,3,7,and one decimal point.

What are the types of the Number?

There are many numbers in algebra such as positive Integer, floating point number and so on.

Number

What is Integer?

Integer is a number that can be written without a fractional component. Integers are generally denoted as Z and can be represented as follows: Z= {∞……-8,-7,-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8,……∞}

Logic to Count Number of Digits in an Integer

  • Step 1: Firstly, the number will be entered by the user. Suppose we declare the variable 'n' and stores the integer value in the 'n' variable.
  • Step 1: We will create a while loop that iterates until the value of 'n' is not equal to zero. Suppose the value of 'n' is 436.
  • Step 2: When the first iteration executes, the value of 'n' will be 43, and the value of count will be incremented to 1.
  • Step 3: When the second iteration executes, the value of 'n' will be 4, and the value of count will be incremented to 2.
  • Step 3: When the third iteration executes, the value of 'n' will be 0, and the value of count will be incremented to 3.
  • Step 3: After the completion of the third iteration, the value of 'n' becomes 0, and loop is terminated as it does not satisfy the condition (n!=0).

Demonstration to Count Number of Digits in an Integer

Digits Count

C Program to Count Number of Digits in an Integer

#include <stdio.h>
int main()  
{  
   int n;  // variable declaration  
   int count=0;   // variable declaration  
   printf("Enter a number");  
   scanf("%d",&n);  
   while(n!=0)  
   {  
       n=n/10;  
       count++;  
   }  
     
   printf("\nThe number of digits in an integer is : %d",count);  
    return 0;  
} 

The output of the Program with code:

Enter a number = 436

The number of digits in above integer is : 3

Count digits

Explanation of the C Program to Count Number of Digits in an Integer with output.

The integer entered by the user is stored in variable n. Suppose user entered 436 Then the while loop is iterated until the test expression n! = 0 is evaluated to 0 (false). After the first iteration, the value of n will be 43 and the count is incremented to 1. After the second iteration, the value of n will be 4 and the count is incremented to 2. After the third iteration, the value of n will be 0 and the count is incremented to 3. After the fourth iteration, the value of n will be 0 and the count is incremented to 4. Then the test expression of the loop is evaluated to false and the loop terminates.

Conclusion:

The internal execution of the program depends upon the entered Integer.

After the successful compilation of the program, a message is displayed on the screen as Enter a number in whose digits you want to count.

If you entered 436 then again a message displayed as The number of digits in above integer is : 3

 

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..