counting digits of a number

Hi every one I have a question.
I joint a c++ class and they taught us how to make a program that can counts the
digits of a number . But there was something I didnt understand . What does this "c" here do? why did we printed c at the end (cout<<c;)? please help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include <iostream>
#include <conio>
main()
{
int n,c=0;
cin>>n;
while(n>0)
{
n=n/10;
c++;
}
cout<<c;
getch();
}
closed account (48T7M4Gy)
Why don't you type in a multi-digit number a few times and see what answers you get. Then go back and read what the program is doing. eg 3, 32, 512, 1024 then 56789
Hi,
c here is used as a counter.
it basically counts the digit.

lets assume n = 100
when while is entered n=n/10 we get n = 10
and c becomes 1 as c++ is written
then again we enter while as still n>0
now n = n/10 so n = 1
c becomes 2
again the same process is repeated and we get c = 3

so c is actually counting digits and we call it counter.
Topic archived. No new replies allowed.