0

This is a program to find number of digits.

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
 int i = 0, n;
 printf("Enter number: ");
 scanf("%d", &n);
 while ((n / pow(10, i)) != 0) {
 i++;
 }
 printf("%d", i);
}

This program gives 309 as the output (value of i) on any input. However, if I store the value of pow(10, i) in another variable and put it in while loop, I get the correct output. Please help!

chqrlie
152k12 gold badges145 silver badges231 bronze badges
asked Feb 14, 2016 at 16:46
4
  • Have you heard of logarithms? Commented Feb 14, 2016 at 16:50
  • 1
    pow returns double then everything in the while condition is double Commented Feb 14, 2016 at 16:50
  • What is the correct output? Commented Feb 14, 2016 at 16:53
  • Rather then describe the code that appears to work, why not post that as well so we can see what it is you are trying to do!? Commented Feb 14, 2016 at 17:19

2 Answers 2

1

C++ uses the most precise type (when types are mixed) when doing a calculation or evaluation, and here you are effectively mixing a double with an int. You are dividing a user input number by a very large exponential number.

This in theory will never be zero, no matter how big the divisor gets. So, the theoretical result should be infinite. However, due to the limits of a double (which is returned by pow), it will still eventually approximate zero.

If you store the return of pow in an integer, you will no longer be mixing types and will effectively be working out the number of digits or what the approximate log base 10 is (any integer divide by a larger integer will equal zero).

answered Feb 14, 2016 at 17:16
Sign up to request clarification or add additional context in comments.

Comments

0

Change:

while( ( n / pow(10,i) )!=0 ){

To:

while( ( n / pow(10,i) ) >= 1 ){

pow returns double and therefore the full result is double and the fractional part will be always non-zero until running out of exponent possible values which is 309 (translated to decimal, binary is 1024).

answered Feb 14, 2016 at 16:50

5 Comments

Thanks for your reply! , but if i write this: z = n / pow(10,i) ; printf("%lf", z); , the output is 0.000000 . Could you please explain that ?
@MohitGarg: how do you define z in z = n / pow(10,i) ; printf("%lf", z);?
@MohitGarg : by default printf() outputs to 6 decimal places, but a double is good for 15 significant figures. Use ""%.15lf" or better a debugger to see the full value of z.
@Clifford Using "%.15f" shows no significance for values like 1.0e-20 and is verbose for 1.0e+20. Recommend "%.16e" (e vs f) to see 17 significant digits. Ref
@chux : Good point; significant figures != decimal places. As I said though, better observed in a debugger than user defined formatted I/O - because you can get it as wrong as I just did!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.