-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathMain.java
35 lines (27 loc) · 970 Bytes
/
MathMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;
public class MathMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a begin number: ");
int begin = sc.nextInt();
System.out.print("Enter an end number: ");
int end = sc.nextInt();
sc.close();
long sum = getSumBetween(begin, end);
System.out.printf("Sum between %d and %d : %,d%n", begin, end, sum);
long product = getProductBetween(begin, end);
System.out.printf("Product between %d and %d : %,d%n", begin, end, product);
}
private static int getSumBetween(int x, int y) {
int sum = 0;
for(int i = x; i <= y; i++)
sum+= i;
return sum;
}
private static int getProductBetween(int x, int y) {
int Product = 1;
for(int i = x; i <= y; i++)
Product*= i;
return Product;
}
}