-9
public class A{
public static void main(String[] args) {
    int a;
    int b;int c=0;
    for(a=100;a<=999;a++){
        for(b=100;b<=999;b++){
            int n=a*b;
            int temp=n;
            while(n>0){
                int r=n%10;
                c=(c*10)+r;
                n=n/10;
            }
            if(temp==c){
                System.out.println(c);
            }
        }
    }
    }
}

The code compiles well but while running it just skips off everything and exits. Help please. P.S. Problem 4 ProjectEuler

2 Answers2

2

First of all lets format the code to read it easily:

public class A {
    public static void main(String[] args) {
        int a;
        int b;
        int c = 0;
        for (a = 100; a <= 999; a++) {
            for (b = 100; b <= 999; b++) {
                int n = a * b;
                int temp = n;
                while (n > 0) {
                    int r = n % 10;
                    c = (c * 10) + r;
                    n = n / 10;
                }
                if (temp == c) {
                    System.out.println(c);
                }
            }
        }
    }
}

Now we notice that the only blocks keeping us away from the print statement inside the main method are the two for loops, and an if statemet. Examining the two loops, there is nothing obviously wrong with them, so we rule them out and what is left is the if statement. At this point we can asume that temp never equals c. If you can't trace why this conition is not satisfied just by looking at the code you can do some simple debuging using print statements (I.e. printing the variables c and temp before the if to visually examine their values for instance) or use some more advanced debuging tools that you can find on IDEs for instance.

Guides on debuging:

How to debug a Java program without using an IDE?

http://www.vogella.com/tutorials/EclipseDebugging/article.html

fill͡pant͡
  • 1,147
  • 2
  • 12
  • 24
1

It won't output anything because temp==cis false

Zwedgy
  • 320
  • 2
  • 3
  • 17