C Program to Count the Number of Digits in an Integer3 min read

In this tutorial, you will learn how to count the number of Digits in an integer in C programming. You will learn various approaches possible to Count the Number of Digits. But before that, you need to have knowledge of the following topics in C programming.

Explanation:
The following programs simply take the user input and display how many digits are present in that integer.

Example: Consider an Integer “256“, The number of digits present here is 3. Therefore the program displays 3 as a result of the program.

1. C Program to Count the Number of Digits using while loop

The same method is applied if you want to do it with the for a loop. Instead of while loop, use for loop in the calculation part, the rest remains the same

Output:


2. C Program to Count the Number of Digits using math library function

Here, we will not use any loops to calculate the result. We will use one of the inbuilt functions provided by the math library in C language and in a single line. The logarithmic function will be helpful in such a program.

log10(num)+1, where log10() is the predefined function in math.h header file.

Output:


3. C Program to Count the Number of Digits using function

A separate function is created in the program and the integer value is passed as an argument in the program as shown below.

Output:


4. C Program to Count the Number of Digits using recursion

Here static is used to count. As the recursion function calls itself again and again until the condition is full-filled, so static variable count is declared so that its value doesn’t initialize again and again after the first call.

Output:


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …