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

Recommended Answers

All 3 Replies

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.

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

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

That's because you copy/pasted code that is irrelevant to the question from http://pastebin.com/JQeihwdQ

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.