Identify correct operator JAVA - EASY - PART005
Identify correct operator
An expression E is passed as an input to the program. The expression will contain three numbers A, B and C, one equal symbol and one of the mathematical operators + - * /
But the given mathematical operator is incorrect and hence the expression is not valid. Hence the program must identify the correct operator and print that as the output.
PROGRAM IN JAVA:
*****************************************************************************
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
s=s.replace("-","=");
s=s.replace("+","=");
s=s.replace("*","=");
s=s.replace("/","=");
String[] s1=s.split("=");
int a=Integer.parseInt(s1[0]);
int b=Integer.parseInt(s1[1]);
int c=Integer.parseInt(s1[2]);
if(a+b==c)
System.out.print('+');
else if(a-b==c)
System.out.print('-');
else if(a*b==c)
System.out.print('*');
else
System.out.print('/');
}
}
*******************************************************************************
Comments
Post a Comment