+ 1
Help me please!!!
*Multiples* You need to calculate the sum of all the multiples of 3 or 5 below a given number. Task: Given an integer number, output the sum of all the multiples of 3 and 5 below that number. If a number is a multiple of both, 3 and 5, it should appear in the sum only once. Input Format: An integer. Output Format: An integer, representing the sum of all the multiples of 3 and 5 below the given input. Sample Input: 10 Sample Output: 23 I cannot understand the format or even how to do the code. Help please!!!!
14 Answers
0
The answer is here :
import java.util.Scanner;
class SumMultiples {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int num=sc.nextInt();
int sum = 0;
for (int i = 1; i < num; i++) {
if (i % 3 == 0 && i % 5 != 0 || i % 5 == 0 && i % 3 != 0) {
sum += i;
}
}
System.out.println("The sum of multiples of 3 and 5 below " + num + " is: " + sum);
}
}
+ 3
Jivaansh Yadav
That's OK, no offense taken.
I just happen to have a hobby to prove things by example.
+ 1
Can you calculate the same result as the sample output by hand?
If you can, try to write down the steps how you calculated it.
Then, think about how to translate these steps into Java code.
From your profile, it seems you started both Java Introduction and Intermediate.
Concentrate on the Introduction first and study the programming basic.
The introduction course covers the techniques you need to solve this problem.
+ 1
š thank you, and you're right, I'm able to do it better now that I know some of the things you mentioned
+ 1
Jivaansh Yadav
Your if clause works the same as mine, but much longer and difficult to read.
You can check this code and see if both method produce the same result.
https://sololearn.com/compiler-playground/c7T9xBNJMc7V/?ref=app
EDITED:
Actually, your code doesn't work correctly. For example, if the input is 20, your code produce 63, and mine produce 78.
The difference is 15 which your code skipped.
To further test out your code, you can try the Code Coach "Multiples" under the community section.
+ 1
I think Wong Hei Ming is right, but thanks for your help Jivaansh
+ 1
I apologise for my mistake and rudeness that I showed to you
+ 1
Let's just be friends again..
+ 1
Yeah sure np
0
No sir I have not started Java Intermediate. My profile is showing that just because I viewed the course
0
Thank you for helping me
0
Thank you
0
The condition is unnecessarily long.
if (i%3 == 0 || i%5 == 0) can get the job done. // if i is divisible by 3 or i is divisible by 5
It is because we only interested in if the number can be divided by 3 or 5.
And Srinjan, if you are able to write the steps down on the paper, once you know how to get user input (Scanner) and how to use a loop, and know about AND, OR, NOT, you can solve this on your own.
0
For Wong Hei Ming But we are going to get the sum of numbers divisible by 3 and 5 and the numbers that are divisible by both should not be repeated .
So the *unnecessarily long condition* is actually important for the satisfaction and correctness of the program.
Although I appreciate your perception and application of your knowledge.
Happy Coding