Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//Calculates number of digits in an integer using while and do

#include <stdio.h>

int main(void)

{
    int digits  = 0, num;

    printf("Enter a nonnegative integer: ");
    scanf("%d", &num);

    do{
        num /= 10;
        digits++;
      }
    while (num > 0);

    printf("Number of digits in your integer is %d\n", digits);

    return 0;
}


What I have tried:

When i enter 11 digit intiger it shows 1 digit
Posted
Updated 29-Jun-18 8:48am
v2
Comments
Rick York 29-Jun-18 20:11pm    
It would be easier to accept input as a string and then count the numbers.

My guess is that is because you are converting the value to an int.
An int variable stores the actual binary value of the integer it is assigned. In the declaration of int x = 564, the variable x stores the binary value of the integer number 564. An int variable can store 4 bytes of data, which means the value of an int can hold range between -2,147,483,648 and +2,147,483,647

if you entered "1111111111" you would actually count to 10, not 9
 
Share this answer
 
v2
Comments
Member 13892651 30-Jun-18 1:10am    
How would i fix this
Clifford Nelson 1-Jul-18 2:51am    
I am no expert in C, but it appears you need to read a string:
gets( str );
and to get the length
strlen(str );
As I said I am no expert in C


Quote:
When i enter 11 digit intiger it shows 1 digit

With the debugger, you will see what your code is doing, pay attention to variables values.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 13892651 30-Jun-18 1:09am    
So what i can do to fix it
Patrice T 30-Jun-18 1:24am    
Every datatype is limited, you reached integer limit, study available types and choose the one that fit your needs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900