Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

20 Feb 2013

Count the number of digits using while loop

Q) C program to count the number of digits in a number using while loop.

Sol)

#include<stdio.h>

int main()
{
    int num,count=0;

   printf("Enter a number : ") ;
   scanf("%d",&num);
   
   while(num)
      {
          num=num/10;
          count++ ;
       }
   
   printf("\nTotal digits is :  %d",count) ;
   return 0;
}

2 comments:

  1. how to do the same by using for loop?? any idea..

    ReplyDelete
    Replies
    1. follow these steps
      for(int i=num; i>=1; i=num/10)
      {
      count++;
      }

      Delete