0

How to count number in C language ?

For example , I have have input 5 until 7
then it will print 3

or input 2 until 4
then it will print 3

here my code:

#include <stdio.h>
int main()
{
  int n,count=0;
  printf("Enter an integer: ");
  scanf("%d", &n);
  while(n!=0)
  {
      n/=10;             /* n=n/10 */
      ++count;
  }
  printf("Number of digits: %d",count);
}

there is wrong, because i can only put one value .. please help

4
Contributors
3
Replies
16
Views
1 Year
Discussion Span
Last Post by nullptr
0

This is a case for a do ... while construct. Try this:

    #include <stdio.h>
    int main()
    {
      int n,count=0;
      do
      {
          printf("Enter an integer: ");
          scanf("%d", &n);
          if (n != 0)
          {
              n/=10;             /* n=n/10 */
              ++count;
          }
      }
      while (n != 0);
      printf("Number of digits: %d",count);
    }

I also think this is not doing what you want, but it may help.

0

If you just want the 'inclusive count of numbers'

enter beginning number in beg
enter ending number in end
inclusive count of numbers is: end-beg+1

Edited by David W

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.