Java program for simple calculator

Program:-

import java.util.Scanner;

class group{
public static void main(String angt[]){
Scanner data = new Scanner(System.in);
double num1, num2, num;
int sign;
// Reading a sign
System.out.println("Enter 1 for addition 2 for subtraction \n 3 for multiplication 4 for division");
sign=data.nextInt();
System.out.println("Enter 1st number");
num1=data.nextDouble();
System.out.println("Enter 2nd number");
num2=data.nextDouble();
switch(sign)
{
case 1:
num = num1+num2;
System.out.println("Answer"+num);
break;
case 2:
num = num1-num2;
System.out.println("Answer"+num);
break;
case 3:
num = num1*num2;
System.out.println("Answer"+num);
break;
case 4:
num = num1/num2;
System.out.println("Answer"+num);
break;
default:
System.out.println("Invalid sign");
break;
}
}
}


Output:-

Enter 1 for addition 2 for subtraction
3 for multiplication 4 for division
1
Enter 1st number
2
Enter 2nd number
5
Answer7.0

Output:-

Enter 1 for addition 2 for subtraction
3 for multiplication 4 for division
2
Enter 1st number
23
Enter 2nd number
5

Output:-

Enter 1 for addition 2 for subtraction
3 for multiplication 4 for division
3
Enter 1st number
25
Enter 2nd number
6
Answer150.0

Output:-

Enter 1 for addition 2 for subtraction
3 for multiplication 4 for division
4
Enter 1st number
556
Enter 2nd number
9
Answer61.77777777777778

Output:-

Enter 1 for addition 2 for subtraction
3 for multiplication 4 for division
6
Enter 1st number
2
Enter 2nd number
3
Invalid sign




Learn Java:-
Java codes

Java Programs:-
List of Java Programs

One Comment

  1. import java.util.Scanner;
    Scanner data = new Scanner(System.in);
    sign=data.nextInt();
    plzzz demonstrate and explain these lines please...

    ReplyDelete

Add your comments here

Back to Top