0

I'm new in Java, it's my first attempt to write a program: I need to write a program that prints the sum of all positive integers smaller than 1000, that are divided by either 3 or 5. Here is my (poorly) attempt. after compiling it is just receiving numbers and showing them :

import java.util.Scanner;
public class ex1 {
    public static void main(String[] args) {
        int num=1;
        int count = 1;
        while (count <=1000) {
            if (count%3==0|count%5==0){
                count = count+num;
                count++;
            }
        }
        System.out.println(count);
    }
}
4
  • count = count+num; kind of seems wrong for what count "seems" to be used for Commented Mar 17, 2015 at 11:50
  • 1
    You seem to be using count for two purposes - this won't work. You first line should be num += count. Commented Mar 17, 2015 at 11:50
  • 1
    "first java program is not working" - Congratulations, you have your first bug! :-) Commented Mar 17, 2015 at 11:56
  • I think I have reached my "1000th java program not working" Commented Mar 17, 2015 at 12:00

3 Answers 3

3

Given you used a while, I assume you don't know about for loops, so I'll avoid using it.

Your code should:

  1. Your initial sum, before any number, is 0
  2. Iterate (i.e. go through the values) the values from 1 to 1000
  3. If the value is divisible by 3 or 5, add it to a sum.
  4. Print the sum.

Point 1):

int sum = 0;

Point 2):

int value = 1;
while (value <= 1000) {
   //do point 3
   value++;
}

Point 3):

if ((value%3==0) || (value%5==0)) {
  sum = sum + value;
}

Point 4):

System.out.println(sum);

Putting it all together:

int sum = 0;
int value = 1;
while (value <= 1000) {
  if ((value%3==0) || (value%5==0)) {
    sum = sum + value;
  }
  value++;
}
System.out.println(sum);

Your main error is in using count both for the sum and for the value check of the while condition. The misusage of the single pipe as or is also a mistake.

Hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

In fact, the use of | instead of || makes no practical difference here. It is at worse a "stylistic mistake".
1
public class Test {

      public static void main(String[] args) {
            int num=1;
            int sum=0;
            while (num <=1000) {
                if (num%3==0||num%5==0){
                    sum = sum +num;
                }
                num++;
            }
            System.out.println(sum);
        }
    }

1 Comment

Yea great. How about explaining what the problem was? In words?
0

You have put your count++ inside if block. That means if the number is divisible by 3 or 5 then you are incrementing the count . Place it outside of your if block. I have re-write your code as follows -

import java.util.Scanner;

public class ex1 {
    public static void main(String[] args) {

        int sum = 0;
        int count = 1;

        while (count <=1000) {
            if (count%3==0||count%5==0){
                sum = sum + count;
            }
            count++;

        }
        System.out.println(sum);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.