Simple Calculator Command ID:2586 JAVA
A string S is passed as input. S will contain two integer values separated by one of these alphabets - A, S, M, D where
- A or a is for addition
- S or s is for subtraction
- M or m is for multiplication
- D or d is for division
The program must perform the necessary operation and print the result as the output. (Ignore any floating point values just print the integer result.)
Input Format:
The first line contains S.
Output Format:
The first line contains the resulting integer value.
Boundary Conditions:
Length of S is from 3 to 100.
Example Input/Output 1:
Input:
5A11
Output:
16
Explanation:
As the alphabet is A, 5 and 11 are added giving 16.
Example Input/Output 2:
Input:
120D6
Output:
20
Example Input/Output 3:
Input:
1405d10
Output:
140
JAVA PROGRAM
import java.util.*; | |
public class Hello { | |
public static void main(String[] args) { | |
//Your Code Here | |
Scanner sc=new Scanner(System.in); | |
String s=sc.nextLine(); | |
char[] s1=s.toCharArray(); | |
char ch='a'; | |
int n=s.length(),f=0; | |
String sam1="",sam2=""; | |
for(int i=0;i<n;i++) | |
{ | |
if(Character.isLetter(s1[i])) | |
{ | |
ch=s1[i]; | |
f=i; | |
break; | |
} | |
sam1=sam1+s1[i]; | |
} | |
for(int j=f+1;j<n;j++) | |
{ | |
sam2=sam2+s1[j]; | |
} | |
int a=Integer.parseInt(sam1); | |
int b=Integer.parseInt(sam2); | |
if(ch=='a' || ch=='A') | |
{ | |
System.out.print(a+b); | |
} | |
else if(ch=='s' || ch=='S') | |
{ | |
System.out.print(a-b); | |
} | |
else if(ch=='M' || ch=='m') | |
{ | |
System.out.print(a*b); | |
} | |
else if(ch=='D' || ch=='d') | |
{ | |
System.out.print(a/b); | |
} | |
} | |
} |
Comments
Post a Comment