This is a Java Program to Calculate the Sum, Multiplication, Division and Subtraction of Two Numbers.
Enter any two integers as input. Now choose from the given options to perform various operations on given integers.
Here is the source code of the Java Program to Calculate the Sum, Multiplication, Division and Subtraction of Two Numbers. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Calculate
{
public static void main(String[] args)
{
int m, n, opt, add, sub, mul;
double div;
Scanner s = new Scanner(System.in);
System.out.print("Enter first number:");
m = s.nextInt();
System.out.print("Enter second number:");
n = s.nextInt();
while(true)
{
System.out.println("Enter 1 for addition");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 to Exit");
opt = s.nextInt();
switch(opt)
{
case 1:
add = m + n;
System.out.println("Result:"+add);
break;
case 2:
sub = m - n;
System.out.println("Result:"+sub);
break;
case 3:
mul = m * n;
System.out.println("Result:"+mul);
break;
case 4:
div = (double)m / n;
System.out.println("Result:"+div);
break;
case 5:
System.exit(0);
}
}
}
}
Output:
$ javac Calculate.java $ java Calculate Enter first number:5 Enter second number:2 Enter 1 for addition Enter 2 for subtraction Enter 3 for multiplication Enter 4 for division Enter 5 to Exit 4 Result:2.5 Enter 1 for addition Enter 2 for subtraction Enter 3 for multiplication Enter 4 for division Enter 5 to Exit 3 Result:10 Enter 1 for addition Enter 2 for subtraction Enter 3 for multiplication Enter 4 for division Enter 5 to Exit 5
Sanfoundry Global Education & Learning Series – 1000 Java Programs.
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Check Java Books
- Practice BCA MCQs
- Apply for Java Internship