1

I am a novice programmer, just learning how to use recursions. Sorry if my question is very basic, but I wish to know why my code outputs extra values. I have run the code multiple times, and it outputs extra values. The point of the code is to take "Pi" and take the digits and "x2" it. So, 3.1415 = 6 2 8 2 10. The output that I got is below, thanks for your help!

public class printPi
{
static int x = 1;
static double pi = .314159265359;
public static void main(String[] args)
{   
    piPrinter(pi);
}
public static void piPrinter(double pi01)
{

    if(x!=0)
    {
        pi = pi*10;
        x = (int)(pi%10);
        x=x*2;
        System.out.println(x);
        piPrinter(pi);
    }
    else
    System.out.println("done.");

}

}

My Output:

6
2
8
2
10
18
4
12
10
6
10
16
18
18
18
18
12
12
12
0

done.(With new lines between each #)

9
  • Possible duplicate of Is floating point math broken? Commented Nov 15, 2016 at 4:05
  • Your code correctly does what you say, namely doubling each digit in Pi. The problem is that multiplying pi by two doesn't mean just doubling each digit, because the overflow from each position needs to be forwarded to the higher tens place. Your code doesn't do this. Commented Nov 15, 2016 at 4:07
  • Your code works as you expect. Just change the value of pi to .31415 Commented Nov 15, 2016 at 4:19
  • +denis, it has to work for the exact value that i gave Commented Nov 15, 2016 at 4:27
  • As a novice programmer I suggest getting familiar with you IDE's debugger. The debugger will allow you to step through each line of code and visualize what each variable is set to at each point. This will help you deduce where you have made your mistakes. It is a very powerful tool once you figure out how to effectively use it Commented Nov 15, 2016 at 4:36

2 Answers 2

1

Try to evaluate only the digit at the left of the dot and "remove" it after the println.

like this:

public class printPi
{

    public static void main(String[] args)
    {   
        double pi = 3.14159265359;
        piPrinter(pi);
    }
    public static void piPrinter(double pi01)
    {

        if ((int) pi01 != 0) {

            System.out.println((int) pi01 * 2);
            piPrinter((pi01 - (int) pi01) * 10);

        } else {
            System.out.println("done.");
        }

    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

1) Your pi value is changing every time so I set a count to limit the iteration over the number.

2) Since you can't set precision to Double,(pi*=10) is not getting the right output, but you could use BigDecimal to this purpose.

piPrinter:

import java.math.BigDecimal;
import java.math.RoundingMode;

public class printPi
{
    static int x = 1;
    static double pi = .314159265359;
    static int piLength = Double.toString ( pi ).length ( ) -1 ;

    public static void main(String[] args)
    {   
        piPrinter(pi, 1);
    }
    public static void piPrinter(double pi01, int count)
    {
        if(count < piLength)
        {
            pi = pi*10;
            Double truncated = BigDecimal.valueOf(pi)
            .setScale(3, RoundingMode.HALF_UP)
            .doubleValue();        
            x = (int)(truncated%10);
            x=x*2;
            System.out.println ( x );
            piPrinter(pi, count+1);
        }
        else
            System.out.println("done.");

    }

}

I/O Examples:

static double pi = .314159265359;

6 2 8 2 10 18 4 12 10 6 10 18 done.

static double pi = .31415;

6 2 8 2 10 done.

2 Comments

I'm so sorry, but is there a way to do it using the basic methods only? My java teacher gives an instant 0 to any code that he has not taught yet. Can you possibly show a simpler version? Thank you so much for your consideration! Note : This is not an assignment, but just a challenge problem he gave for fun.
@Dante solution is pretty much simplier than mine. Stick with that. Good luck.

Your Answer

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.