Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

C Program to Find Number of Digits in an Integer

Last Updated on May 12, 2023 by Prepbytes

In this article, we’ll look at different ways to count the number of digits in an integer in C. We must basically count the number of digits in an integer and return the number of digits.

Various Methods to Count the Number of Digits in an Integer in C

This issue can primarily be addressed using three approaches:

  • Making Use of Loops
  • Using the mathematical formula
  • Making use of recursive methods

Let us now go over each method in detail.

Using Loops in C to Count the Number of Digits in an Integer

  1. To begin, the user will enter the number. Assume we declare the variable ‘n’ and store the integer value in it.
  2. We’ll make a while loop that iterates until the value of ‘n’ is greater than zero.
  3. Assume that the value of ‘n’ is 123.
  4. When the first iteration is completed, the value of ‘n’ is 123, and the value of the count is increased to 1.
  5. The value of ‘n’ will be 12 when the second iteration runs, and the value of the count will be increased to 2.
  6. When the third iteration runs, the value of ‘n’ will be 1, and the value of the count will be increased to 3.
  7. After the third iteration, the value of ‘n’ becomes zero, and the loop is terminated because it does not satisfy the condition (n!=0).

Code Implementation to Find the Number of Digits in an Integer Using Loops in C

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

Output

Enter any number: 1000
The number of digits are: 4 

Counting the Number of Digits in an Integer using Math in C

  1. First, we ask the user to enter an integer that we must calculate.
  2. The formula floor(log10(n))+1 is then used.
  3. Print the outcome

Code Implementation to Find the Number of Digits in an Integer Using Math in C

#include<stdio.h>
#include<math.h>

int main(){
    int n;
    printf("Enter any number:\n");
    scanf("%d",&n);
    int x = floor(log10(n))+1;
    
    printf("No. of digits = %d", x);

}

Output

Enter any number:
1000
No. of digits = 4 

Counting the Number of Digits in an Integer using Recursion in C

  1. Start the program.
  2. Declare an integer variable num to hold the input number and another integer variable count to keep track of the number of digits.
  3. Request that the user enter the value of ‘num’.
  4. Use the function func(num) and save the result in the variable count.
  5. The value of ‘count’ should be printed as the number of digits in the input number.
  6. Finish the program.
  7. ‘func(int n)’ function:
    1. Declare a static integer variable called ‘counter’ to keep track of the number of digits.
    2. Check to see if the value of ‘n’ is greater than zero.
    3. If ‘n’ is greater than zero, increment the value of ‘counter’ by one and recursively call the function ‘func(n/10)’.
    4. Return the value of ‘counter’ if ‘n’ is less than or equal to 0.
  8. Put an end to the function.

Code Implementation to Find the Number of Digits in an Integer Using Recursion in C

#include <stdio.h>  
int main()  
{  
    int num;   
    int count=0;   
    printf("Enter a number");  
    scanf("%d",&num);  
   count=func(num);  
   printf("Number of digits is : %d", count);  
   return 0;  
}  
int func(int n)  
{  
  static int counter=0; 
  if(n>0)  
  {  
      counter=counter+1;  
      return func(n/10);  
  }  
    else  
    return counter;  
}

Output

Enter a number 500
500
Number of digits is : 3

Conclusion
This article looks at three different methods for counting the number of digits in an integer in the C programming language. Loops, math formulas, and recursive methods are used in the three approaches. A while loop is used in the loop method to iterate through the digits of the integer until the value becomes zero. The formula floor(log10(n))+1 is used to calculate the number of digits in the math formula method. Finally, a function is created for the recursive method that recursively calls itself to count the digits of the integer. Each method’s code implementation and output are included in the article.

Frequently Asked Questions (FAQs)

Q1. In C, how do you find the number of digits in an integer?
Ans. The number of digits can be calculated using log10(num)+1, where log10() is math. h predefined function.

Q2. How do I get the last two digits of an integer C?
Ans.

  • Consider any number as input.
  • Divide the input by 100 to get the remainder.
  • The output is the remainder obtained.

Q3. How do you count digits from one to one hundred?
Ans. If we write down the numbers from 1 to 100, we will need 192 digits: one for each of the numbers 1 to 9, two for each of the ninety numbers 10 to 99, and three for the number 100.

Leave a Reply

Your email address will not be published. Required fields are marked *