1

Targer - need to count how many numbers with 6 digit have same summ of left and right 3 digit

What done - prepared 3 methods:

1-st - counting summ of left 3 digit from number - works ok

private static int counterLeft3(int i) {
    int digitCounter=0;
    int summLeft3=0;
    while (digitCounter!=3){
        summLeft3=summLeft3+(i%10);
        i=i/10;
        digitCounter++;
    }
    return summLeft3;
}

2-nd - counting summ of right 3 digit from number - works ok

 private static int counterRight3(int i) {
    int summRight3=0;
    int buffer;
    int counter=0;
    while (counter!=3){
        summRight3=summRight3+(i%10);
        i=i/10;
        counter++;
    }
    buffer =i;
    summRight3=0;
    while (buffer!=0){
        summRight3=summRight3+(buffer%10);
        buffer=buffer/10;
    }
    return summRight3;
}

3-dr - cycle for counting q-ty of numbers - allways return 0. thnink - my mistake thomthing here :

private static void summCounter() {
    int counter=0;
    for (int i=111110; i<1000000; i++){
        if (counterRight3(i)==counterLeft3(i)){ 
        }
        counter = counter++;
    }
    System.out.println("Q-ty is " + counter);
}

debug - example

enter image description here

result for 1-st method

enter image description here

result for 2-nd method

enter image description here

Question - what wrong with 3-rd method, why it allways return just 0 and counter never increase?

hbk
  • 10,908
  • 11
  • 91
  • 124

1 Answers1

2

In your 3rd method the assignment: -

counter = counter++;

has no effect on counter value. After this assignment, counter remains 0 only. You need to remove the assignment part, and just have increment part: -

counter++;

And you need to do the increment inside your if block only when your left and right sum is equal.

See also: -

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525