-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSummationsCalculator.java
More file actions
60 lines (59 loc) · 1.55 KB
/
SummationsCalculator.java
File metadata and controls
60 lines (59 loc) · 1.55 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*Create a program that takes 3 inputs, a lower bound, an upper bound and the expression. Calculate the sum of that range based on the given expression and output the result.
For Example:
Input: 2 4 *2
Output: 18 (2*2 + 3*2 + 4*2)
*/
import java.util.Scanner;
public class SummationsCalculator{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter the first number");
int num1=in.nextInt();
System.out.println("Enter the last number");
int num2=in.nextInt();
System.out.println("Enter the expression");
char exp=in.next().charAt(0);
System.out.println("Enter the expression number");
int exp_num=in.nextInt();
int res=0,sum=0;
switch(exp){
case '+':
for (int i=num1;i<=num2;i++) {
res=i+exp_num;
sum+=res;
}
System.out.println("The result="+sum);
break;
case '-':
for (int i=num1;i<=num2;i++) {
res=i-exp_num;
sum+=res;
}
System.out.println("The result="+sum);
break;
case '*':
for (int i=num1;i<=num2;i++) {
res=i*exp_num;
sum+=res;
}
System.out.println("The result="+sum);
break;
case '/':
for (int i=num1;i<=num2;i++) {
res=i/exp_num;
sum+=res;
}
System.out.println("The result="+sum);
break;
case '%':
for (int i=num1;i<=num2;i++) {
res=i%exp_num;
sum+=res;
}
System.out.println("The result="+sum);
break;
default :System.out.println("Wrong expression");
break;
}
}
}