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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More