In this post you will learn Basic calculator operations program by using Java. Basic calculator Operations means additions,subtractions,multiplication and division. To implement this operation i am using Java programming Language. The following code will help to implement calculator operations.
package seleniumsession1;
import java.util.Scanner;
public class CalculatorDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("enter two numbers");
double first=sc.nextDouble();
double second=sc.nextDouble();
System.out.println("enter an operator(+,-,*,/):");
char ch=sc.next().charAt(0);
double result;
switch(ch) {
case '+':
result=first+second;
break;
case '-':
result=first-second;
break;
case '*':
result=first*second;
break;
case '/':
result=first/second;
break;
default:
System.out.println("incorrect operator");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, ch, second, result);
}
}
Output:
Enter two numbers:
100 200
enter any operator:
+
100.0+200.0=300.0
package seleniumsession1;
import java.util.Scanner;
public class CalculatorDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("enter two numbers");
double first=sc.nextDouble();
double second=sc.nextDouble();
System.out.println("enter an operator(+,-,*,/):");
char ch=sc.next().charAt(0);
double result;
switch(ch) {
case '+':
result=first+second;
break;
case '-':
result=first-second;
break;
case '*':
result=first*second;
break;
case '/':
result=first/second;
break;
default:
System.out.println("incorrect operator");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, ch, second, result);
}
}
Output:
Enter two numbers:
100 200
enter any operator:
+
100.0+200.0=300.0
No comments:
Post a Comment