-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathQuestion91.java
More file actions
38 lines (38 loc) · 1.25 KB
/
Question91.java
File metadata and controls
38 lines (38 loc) · 1.25 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
import java.util.Scanner;
public class Question91{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String input = sc.nextLine(); // Read as string, e.g., 5+6
// Declare and initialize the required variable(s)
int i=0;
int j=0;
double output=0;
// Split the input string into character array
char seq[] = input.toCharArray();
/*
Use some method to separate the two operands
and then perform the required operation.
*/
for(int a=0; a<seq.length; a++){
if(seq[a]=='+'){
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,seq.length));
output = (double)i+j;
}else if(seq[a]=='-'){
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,seq.length));
output = (double)i-j;
}else if(seq[a]=='/'){
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,seq.length));
output = (double)i/j;
}else if(seq[a]=='*'){
i= Integer.parseInt(input.substring(0,a));
j= Integer.parseInt(input.substring(a+1,seq.length));
output = (double)i*j;
}
}
// Print the output as stated in the question
System.out.print(input+" = " + Math.round(output));
}
}