Sign in to follow this  

number of diggits in a integer

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quote:
Original post by Nice Coder
for(int i = 0; x > 9; x /= 10, i++);

Would that work?


i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me

Share this post


Link to post
Share on other sites
Depends on how the number is represented. Decimal,hex,bin,octal or others system could produce different numbers of digits.

Lets take the number 123d in decimal:

-123d in decimal(123) and has 3 digits,
-123d in hex(7Bh) has 2 digits,
-123d in binary(1111011b) has 7 digits
-123d in octal(173o) has 3 digits.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
i will be off by one and not accesible outside the scope of the for loop.


Not to mention that it's horribly inefficient to use a loop to do something for which a simple formula exists.

Share this post


Link to post
Share on other sites
Quote:
Original post by deathkrush
The best way to do this is to convert the number to a string and find the length of the string.

(just kidding)

:)
Ever read The Daily WTF? It's amazing how often the "convert to string" approach is taken in real-world applications by programmers who should know better.

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmadog
Ever read The Daily WTF? It's amazing how often the "convert to string" approach is taken in real-world applications by programmers who should know better.


I was just going to say that. It's funny how the first solution that comes to mind is usually a WTF one.

Share this post


Link to post
Share on other sites
Quote:
Original post by Nanook
but seriously.. im a newb.. why? :D


Because it's equivalent to printing out the number, putting the print out on a wooden table, taking a picture, scanning the picture and then using an image recognition program to find the number of digits. :-)

Seriously, though, if there is a simple formula to calculate the number of digits, there is no need to get wooden tables involved. In this case, not only the string-based solution is much slower, the professor probably wouldn't accept it because by using strings you ignore properties of numbers. Knowing these properties will help you in programming.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
Quote:
Original post by Nice Coder
for(int i = 0; x > 9; x /= 10, i++);

Would that work?


i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me


Ok so to fix it:

for(int i = 1; x > 9; x /= 10; i++);

There... now "i" is the number of digits, but assuming that x is a positive number.

...and it IS accessible outside the scope of the loop. Why do you think it's not?

Share this post


Link to post
Share on other sites
Quote:
Original post by superdeveloper
Quote:
Original post by Palidine
Quote:
Original post by Nice Coder
for(int i = 0; x > 9; x /= 10, i++);

Would that work?


i will be off by one and not accesible outside the scope of the for loop.

but either way the important thing, Nanook, is that you be able to explain to your teacher why it works...

If it's a homework assignment then try thinking about it for a while. It wouldn't have been assigned if you didn't have the tools to solve it.

-me


Ok so to fix it:

for(int i = 1; x > 9; x /= 10; i++);

There... now "i" is the number of digits, but assuming that x is a positive number.

...and it IS accessible outside the scope of the loop. Why do you think it's not?


Only if you are using VC++ 6.

Share this post


Link to post
Share on other sites
Quote:
Original post by superdeveloper
...and it IS accessible outside the scope of the loop. Why do you think it's not?


It won't be, if the compiler is fully standards-compliant. It shouldn't be relied upon.

Share this post


Link to post
Share on other sites
I don't see where the i fits into that solution . . . except as a counter, but still. My solution is nearly the same, just slightly different.

digits = 0
for(count = 10; count<goal_num; count *= 10, digits++);

When count is greater than the goal number it'll stop, so digits will equal the number of digits. The only problem, I guess, is for negative numbers.

Share this post


Link to post
Share on other sites
Dealing with negatives isn't a big deal. just use the absolute value of the number in your check.


#include <iostream>
#include <math.h>
void main()
{
using namespace std;

int number, digits = 1;
cin >>number;

while(abs(number)>=9)
{
number = number/10;
digits++;
}

cout<<digits<<endl;
}


Share this post


Link to post
Share on other sites
My basic and simple approach would be like calculating it to a scientific number. that shouldnt be too hard. and how do you get the digits from there??? n * 10^f is your scientific number, numDigits = f+1.


*SPOILER WARNING!!!!*


if you are lazy and would just like the answer it is:
numDigits = (int)log10(abs(num))+1;

that is if the int casting rounds down automatically, if it doesn't im sure you can think of a way to get it to round down.

And for all you people who can't think of this, shame on you, this is super easy

NOTE:if you would like to figure out the number of digits in another base, just do log10(abs(num))/log10(otherBase)+1

Share this post


Link to post
Share on other sites
Quote:
Original post by Nypyren
num_digits = ceil( log2(maximum_value) / log2(display_radix) );

This fails for numbers that are exact powers of 10 (eg. 1000, which reports 3 digits). As suggested above a few times, you need to use floor() + 1, not ceil()

Share this post


Link to post
Share on other sites

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this