Help me please!!! | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 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!!!!

6th May 2024, 10:08 AM
Srinjan
7 ответов
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); } }
27th May 2024, 4:36 PM
Jivaansh Yadav
Jivaansh Yadav - avatar
+ 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.
6th May 2024, 11:06 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
No sir I have not started Java Intermediate. My profile is showing that just because I viewed the course
6th May 2024, 11:15 AM
Srinjan
0
Thank you for helping me
6th May 2024, 11:16 AM
Srinjan
0
Thank you
28th May 2024, 3:22 AM
Srinjan
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.
28th May 2024, 3:37 AM
Wong Hei Ming
Wong Hei Ming - avatar
0
👍 thank you, and you're right, I'm able to do it better now that I know some of the things you mentioned
28th May 2024, 3:46 AM
Srinjan