41 : C Program to Count number of digits of entered Number

Check out the complete list of c-programs : C Program List    



This C program accepts a integer and counts the number of digits.

Variables :

1. number : Datatype int : Holds value of number inputted by the user.

2. temp_var : Datatype int : To hold temporary values.

3. sum : Datatype int : Holds result ie no. of digits.



Functions :

printf() : is used to display something on the console/screen. Format specifier %d in printf function is used to display a int variable on screen and %f for float. \n is used to add a newline

scanf() : is used to fetch data inputted by the user on the console/screen using keyboard. %d in scanf is indicates that inputted text is of type int.



Logic :


   while(number >= 1 ) {
       
      temp_var = number%10; //get unit place digit
      sum = sum + 1; //increment the digit count by 1
      number = number/10; //remove the last digit
       
       
   }









/*
 * 1000+ C programs + tutorials
 *
 * 
 * 41_count_number_of_digits_c_program.c
 *
 *
 *  Created on: Oct 20, 2014
 *  Author: Code2care.org
 */

#include 
//#include 



void main() {

   int number,sum,temp_var;
  

//    clrscr();

    printf("C Program to Number of digits for a Number Entered");

    printf("\n\n Enter Number : ");
    scanf("%i", &number);
    
    printf("\n\n Number : %i",number);
     
     
   //Logic to calculate number of digits
   sum = 0;
   while(number >=1 ) {
       
      temp_var = number%10; //get unit place digit
      sum = sum + 1; 
      number = number/10; //remove the last digit
       
       
   }

   printf("\n\n Total Digits : %i", sum);
    


//	getch();




}









The best way to learn C programming is to practice more and more of programs . Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.

BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.