Java Program to Perform Arithmetic Operations

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.

  1. import java.util.Scanner;
  2. public class Calculate 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int m, n, opt, add, sub, mul;
  7.         double div;
  8.         Scanner s = new Scanner(System.in);
  9.         System.out.print("Enter first number:");
  10.         m = s.nextInt();
  11.         System.out.print("Enter second number:");
  12.         n = s.nextInt();
  13.         while(true)
  14.         {
  15.             System.out.println("Enter 1 for addition");
  16.             System.out.println("Enter 2 for subtraction");
  17.             System.out.println("Enter 3 for multiplication");
  18.             System.out.println("Enter 4 for division");
  19.             System.out.println("Enter 5 to Exit");
  20.             opt = s.nextInt();
  21.             switch(opt)
  22.             {
  23.                 case 1:
  24.                 add = m + n;
  25.                 System.out.println("Result:"+add);
  26.                 break;
  27.  
  28.                 case 2:
  29.                 sub = m - n;
  30.                 System.out.println("Result:"+sub);
  31.                 break;
  32.  
  33.                 case 3:
  34.                 mul = m * n;
  35.                 System.out.println("Result:"+mul);
  36.                 break;
  37.  
  38.                 case 4:
  39.                 div = (double)m / n;
  40.                 System.out.println("Result:"+div);
  41.                 break;    
  42.  
  43.                 case 5:
  44.                 System.exit(0);
  45.             }
  46.         }
  47.     }
  48. }

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
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.