0

im having a problem in my java exercice the question is saying The program will end when the sum of even is >= 50 or the sum of odd is >= 49.

so while solving it i tried to use

while (sumeven < 50 || sumodd < 49 )

and it didnt worked at all but when i checked the solution they use

while (evensum <= 50 && oddsum<=49)

and it worked ( gave same answeres like the sample run)

so my question is did i misunderstood it ? or the question have some kind of a wrong given. thank you for your time

update:

the code :

package sample2;
import java.util.Scanner;

public class Sample2 {

    public static void main(String[] args) {
        Scanner scan=new Scanner(System.in);

        System.out.println("enter the initial value");
        int sumeven=0;
        int sumodd=0;
        int num;
        int initial;
        int div5and2=0;
        initial=scan.nextInt();
        if (initial<=0)
            System.out.println("the number must be strictly positive");
        else{
            if (initial%2==0)
                sumeven=initial;
            else
                sumodd=initial;

            System.out.println("start entering your numbers");
            num=scan.nextInt();
            if (num<=0)
                System.out.println("the number must be strictly positive");
            else{
                while(sumeven<=50||sumodd<=49 )
                {
                    if (num%2==0)
                        sumeven=sumeven+num;
                    else 
                        sumodd=sumodd+num;
                    if (num%5==0 &&num%2==0)
                        div5and2=div5and2+1;

                    num=scan.nextInt();
                    while(num<=0)
                    {
                        System.out.println("the number must be strictly positive");
                        num=scan.nextInt();
                    }
                }

                System.out.println("the sum of even numbers: "+sumeven);
                System.out.println("the sum of odd numbers: "+sumodd);
                System.out.println("the number of integers divided by 2 and 5: "+div5and2);
            }
        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
Ali Zgheib
  • 15
  • 4

7 Answers7

2

This is basic boolean algebra.

The 'opposite' of or is and, so if you want to stop when

"even >= 50 or odd >= 49"

you have to continue on the opposite, which is

"even < 50 and odd < 49".
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
1

while(sumeven<50||sumodd<49 ) means the program will run when sumeven<50 or sumodd<49, which means it will exit when sumeven>=50 and sumodd>=49.

xingbin
  • 27,410
  • 9
  • 53
  • 103
1

When you negate a statement, >= becomes < and <= becomes >. Similarly, AND becomes OR and OR becomes AND. Here, your stopping condition is even >= 50 OR odd >= 49, the negation of this (which is what is required to continue) is even<50 AND odd<49.

Batman
  • 282
  • 3
  • 12
1

You want the program to end when " the sum of even is >= 50 or the sum of odd is >= 49."

So, need something like (in psuedo code):

while (Not( the sum of even is >= 50 or the sum of odd is >= 49))

De Morgan's laws tell us we need to not each part to remove the brackets and switch between and and or:

while (Not( the sum of even is >= 50) and Not( or the sum of odd is >= 49))

Let's try some examples. If the even sum is 50 and the odd sum is 40, you want to stop. If you check

while (sumeven < 50 || sumodd < 49 )

you will keep going since sumeven < 50 is false, but sumodd < 49 is true.

Checking both parts:

while (evensum <= 50 && oddsum<=49)

works.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

while(sumeven<50||sumodd<49 )

In this case, the program did not end if sumeven<50 OR sumodd<49. That is, the program will end when sumeven>=50 AND sumodd>=50.

0

Boolean logic 101:

!(A || B) == !A && !B note the &&.

Essentially if you are looking at a state where either A or B and want to take the compliment of that you must code it as not either A or B which is obviously neither A nor B or not A and not B.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

AIM - The program will end when the sum of even is >= 50 or the sum of odd is >= 49.

Therefore, while (sumeven < 50 || sumodd < 49 ) won't work as the program will end when the sum of even is < 50 or the sum of odd is < 49 which is not the expected behavior.

Therefore, we must loop till

  1. The sum of even is less than or equal to 50
  2. The sum of odd is less than or equal to 49

The overall set of conditions should only be true if all conditions are true therefore we use && operator with the 2 conditions.

theboringdeveloper
  • 1,429
  • 13
  • 17