C Program to Count Number of Digits in a Number

C Program to Count Number of Digits in a Number

C program to count number of digits in a number; Through this tutorial, you will learn how to count or find number of digits in a number in c program using For Loop, While Loop, Functions, and Recursion.

Algorithm to count the number of digits in a number

Use the following steps to write a program to count number of digits in a number; as follows:

  1. Read integer number and store it into a variable.
  2. Initialize another variable to store total digits say count = 0.
  3. If num > 0 then increment count by 1 i.e. count++.
  4. Divide num by 10 to remove the last digit of the given number i.e. num = num / 10.
  5. Repeat step 3 to 4 till num > 0 or num != 0.
  6. Print number of count in a number

Programs to Count Number of Digits in a Number in C

Let’s use the following program to count number of digits in a number in c using for loop, while loop, function and recursion:

  • C Program to Count Number of Digits in a Number using While Loop
  • C Program to Count Number of Digits in a Number using For Loop
  • C Program to Count Number of Digits in a Number Using Functions
  • C Program to Count Number of Digits in a Number Using Recursion

C Program to Count Number of Digits in a Number using While Loop

/* C Program to Count Number of Digits in a Number using While Loop */
#include <stdio.h>

int main()
{
  int Number, Reminder, Count=0;

  printf("\n Please Enter any number :- ");
  scanf("%d", &Number);

  while(Number > 0)
  {
     Number = Number / 10;
     Count = Count + 1;  
  }

  printf("\n Number of Digits in a Given Number = %d", Count);
  return 0;
}

The output of the above c program; as follows:

Please Enter any number :- 123456
Number of Digits in a Given Number = 6

C Program to Count Number of Digits in a Number using For Loop

/* C Program to Count Number of Digits in a Number using For Loop */ 

#include <stdio.h>

int main()
{
  int Number, Reminder, Count;

  printf("\n Please Enter any number :- ");
  scanf("%d", &Number);

  for (Count=0; Number > 0;Number=Number/10)
  {
     Count=Count + 1;  
  }

  printf("\n Number of Digits in a Given Number = %d", Count);

  return 0;
}

The output of the above c program; as follows:

Please Enter any number :- 145698
Number of Digits in a Given Number = 6

C Program to Count Number of Digits in a Number Using Functions

/* C Program to Count Number of Digits in a Number using While Loop */
#include <stdio.h>

int Count_Of_Digits (int Number)
{
 int c = 0;
 
  while(Number > 0)
  {
     Number = Number / 10;
     c = c + 1;  
  }

  printf("\n Number of Digits in a Given Number = %d", c);
}

int main()
{
  int Number;

  printf("\n Please Enter any number :- ");
  scanf("%d", &Number);
  
  Count_Of_Digits (Number);

  return 0;
}

The output of the above c program; as follows:

Please Enter any number :- 123456789
Number of Digits in a Given Number = 9

C Program to Count Number of Digits in a Number Using Recursion

/* C Program to Count Number of Digits in a Number Using Recursions */

#include <stdio.h>

int Count_Of_Digits (int); 

int main()
{
  int Number, Count = 0;

  printf("\n Please Enter any number :- ");
  scanf("%d", &Number);

  Count = Count_Of_Digits (Number);

  printf("\n Number of Digits in a Given Number = %d", Count);
  return 0;
}

int Count_Of_Digits (int Number)
{
  static int Count =0;

  if(Number > 0)
  {
     Count = Count + 1;  
     Count_Of_Digits (Number / 10);
  }

 return Count;
}

The output of the above c program; as follows:

Please Enter any number :- 41236
Number of Digits in a Given Number = 5

Recommended C Programs

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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