-1

I have task: to make a java-programm which will print numbers from 000001 to 999999(it's a tram's tickets numbers) and count all numbers which have equilly left and right sides(for example: 024033, 125008, 531900 etc.). I wrote the code:

 public class Cycle13 {

    public static void main(String[] args) {
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 1;
        int current_b = 0;
        int current_c = 0;
        int current_d = 0;
        int current_e = 0;
        int current_f = 0;
        int counter = 0;

        for (int i = 1; i <= 1000000; ++i) {

            if (f >= 10) {
                current_f = 10;
                f = 0;
            }
            if (current_f >= 10) {
                ++e;
                current_f = 0;
            }

            if (e >= 10) {
                current_e = 10;
                e = 0;
            }
            if (current_e >= 10) {
                ++d;
                current_e = 0;
            }
            if (d >= 10) {
                current_d = 10;
                d = 0;
            }
            if (current_d >= 10) {
                ++c;
                current_d = 0;
            }
            if (c >= 10) {
                current_c = 10;
                c = 0;
            }
            if (current_c >= 10) {
                ++b;
                current_c = 0;
            }
            if (b >= 10) {
                current_b = 10;
                b = 0;
            }
            if (current_b >= 10) {
                ++a;
                current_b = 0;
            }
            if (a >= 10) {
                break;
            }
            System.out.print(a);
            System.out.print(b);
            System.out.print(c);
            System.out.print(d);
            System.out.print(e);
            System.out.print(f);

            ++f;
            if (a + b + c == d + e + f) {
                counter++;
            }
            System.out.println();
        }

        System.out.print(counter);
    }
 }

And this code has a strange behavior despite it maked right. I'm sure of it because if set up the condition i<=10000 (i.e four zeros) in the cycle for the code works correctly. If replace the cycle for on the cycle while it would be the same result. Who knows what the problem is?

singhakash
  • 7,891
  • 6
  • 31
  • 65
bbirdoff
  • 1
  • 1
  • 2
    I cant say about others but your question is unclear for me – singhakash Oct 28 '15 at 10:47
  • http://stackoverflow.com/questions/14743165/simple-number-to-array-with-individual-digits – assylias Oct 28 '15 at 10:48
  • 1
    "Strange behavior" - what strange behavior? What does it do that you don't expect? By the way, the word is "loop", not "cycle". Can you make your question clearer? – RealSkeptic Oct 28 '15 at 10:58
  • Thank you for constructive criticism. I'm confused because while my program is running it prints numbers from 000001 in console(i.e like it must be: 000001, 000002, 000003 etc). But, when program is terminated there are console displays numbers from 990001(i.e 990001, 990002, 990003 etc). – bbirdoff Oct 28 '15 at 15:21

1 Answers1

0

Besides your grammar and hardly understanding your problem, running this code will get your expected results without any "strange" behavior ;-)

public class Cycle13 {
    public static void main(String[] args) {
        int counter = 0;
        for(int a = 0; a < 10; a++){
            for(int b = 0; b < 10; b++){
                for(int c = 0; c < 10; c++){
                    for(int d = 0; d < 10; d++){
                        for(int e = 0; e < 10; e++){
                            for(int f = 0; f < 10; f++){
                                if(a+b+c == d+e+f){
                                   counter++;
                                }
                                System.out.print(a);
                                System.out.print(b);
                                System.out.print(c);
                                System.out.print(d);
                                System.out.print(e);
                                System.out.print(f);
                                System.out.println();
                            }
                        }
                    }
                }
            }
         }
      System.out.print(counter);
   }
}
BadK
  • 307
  • 3
  • 12