Skip to content

Commit a76dfbb

Browse files
committed
Sum of all the multiples of 3 or 5 is added
1 parent 22bf63b commit a76dfbb

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package problem1;
2+
3+
public class SumOfMultiples {
4+
int sum = 0;
5+
6+
public void addMultiples(int n) {
7+
for (int i = 1; i < n; i++) {
8+
if (i % 3 == 0 || i % 5 == 0) {
9+
sum = sum + i;
10+
}
11+
}
12+
System.out.println("The sum of all the multiples of 3 or 5 below " + n + " is " + sum);
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package problem1;
2+
import java.util.Scanner;
3+
4+
public class SumOfMultiplesMain {
5+
public static void main(String[] args) {
6+
Scanner scanner = new Scanner(System.in);
7+
int n;
8+
9+
SumOfMultiples sum = new SumOfMultiples();
10+
11+
System.out.println("Enter the number : ");
12+
n = scanner.nextInt();
13+
14+
sum.addMultiples(n);
15+
}
16+
}

0 commit comments

Comments
 (0)