Java Program to Make a Simple Calculator Using if else

In this program we going to make a calculator with simple if else program to do operations like addition, subtraction, multiplication, division.




Program: 


import java.util.Scanner;
public class Calculat {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Enter Data 1 and Data 2 to calc");

double a= in.nextDouble();
double b=in.nextDouble();
System.out.println("Enter sum=0/sub=1/mul=2/div=3/rem=4 operation to do");
double c = 0;
int  ope= in.nextInt();
if(ope==0) c=a+b;
else if(ope==1) c=a-b;
else if(ope==2) c=a*b;
else if(ope==3) c=a/b;
else if(ope==4) c=a%b;
else System.out.println("invalid symbol so incorrect output");
System.out.println("The " +ope +" of " +a +" " +b +" is " +c);
}
}


In this program we are getting 2 datas from user as well as the operation to do and finally we are calculating result with if else statement.

0 comments:

Post a Comment