-2

/* when I run this code there is no error in fact output generated is also correct but I want to know what is the logical error in this code? please can any one explain what is the logical error. */

class abc
    {
        public static void main(String arg[]){
        int sum=0;
        //for-loop for numbers 50-250
        for(int i=50;i<251;i++){
            // condition to check if number should be divided by 3 and not divided by 9 
            if(i%3==0 & i%9!=0){
                //individual number which are selected in loop
                System.out.println(i);
                //adding values of array so that total sum can be calculated
                sum=sum+i;   
            }   
        }
        //final display output for the code 
        System.out.println("the sum of intergers from 50 to 250 that are multiples of 3 and not divisible by 9 \n"+sum);
    }
}
m2j
  • 1,152
  • 5
  • 18
  • 43
lilly
  • 5
  • 1
  • 2
  • 1
    you are missing another ampersad (&) in the condition. it should be `i%3==0 && i%9!=0` to use logical AND – Rafael Apr 16 '16 at 04:18
  • the logic of your code looks fine. Why are you saying that the result isn't right? – Maljam Apr 16 '16 at 04:23
  • Atlease convert the Home work Query to something else Like you already worked with Lilly – Prasad Apr 16 '16 at 04:27

4 Answers4

2

My philosophy is "less code == less bugs":

int sum = IntStream.rangeClosed(50, 250)
    .filter(i -> i % 3 == 0)
    .filter(i -> i % 9 != 0)
    .sum();

One line. Easy to read and understand. No bugs.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

Change this:

if(i%3==0 & i%9!=0){

to this:

if(i%3==0 && i%9!=0){

& = bitwise and operator

&& = logical operator

Difference between & and && in Java?

Community
  • 1
  • 1
markwatsonatx
  • 3,391
  • 2
  • 21
  • 19
  • 2
    `&` still works as intended, but it isn't subjected to short-circuiting. So your answer doesn't change the result... – Maljam Apr 16 '16 at 04:19
  • Correct. I am taking a guess as to what the OP is referring to as the "logical error" in the code. OP stated in the question that the output was correct. – markwatsonatx Apr 16 '16 at 04:26
  • The two versions of code have identical behaviour. There is no difference whatsoever. There may be a barely measurable (nanoseconds) performance difference favouring `&&` over `&` – Bohemian Apr 16 '16 at 04:59
0

The only problems I saw were:

  • The variable sum was undeclared
  • Use && in place of &

int sum = 0;
for (int i = 50; i <= 250; i++) {
    if (i % 3 == 0 && i % 9 != 0) {
        System.out.println(i);
        sum = sum + i;
    }
}
System.out.println("the sum of intergers from 50 to 250 that are multiples of 3 and not divisible by 9 \n" + sum);
spencer.sm
  • 19,173
  • 10
  • 77
  • 88
0

Well, instead of touching every single value from 50 to 250 like you would do here for(int i=50;i<251;i++), you can consider something like this...

int i = 48;
int sum = 0;
while(i < 250) {
    i += 3;
    if(i%9 != 0)
        sum += i;
}

This is somewhat optimized in the sense that I am skipping over values that I know are not possible candidates.

But, there is a much bigger issue in your code. The following code block prints true, sure. But, it is a bad idea to depend on the & since that is not its job. The & is for bitwise AND whereas the && is for logical AND, which is what you are trying to do.

boolean t = true;
boolean f = false;
System.out.println(f&t);

Why?

In Java, if it is a && operation, as soon as you find the first false, you are sure that the expression will evaluate to false. Meanwhile, in your implementation, it would need to evaluate both sides. f&t will evaluate to false, but the JVM would need to look at both the f and t variables. Meanwhile, on using &&, it wouldn't even need to look at the t.

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43