Java Program to Illustrate the Use of Arithmetic Operators

«
»
This is a Java Program to Illustrate the Use of Arithmetic Operators.

Enter two integers as input. After that we use switch case to choose from different arithmetic operations like addition, subtraction, multiplication, division and modulus to get the desired output.

Here is the source code of the Java Program to Illustrate the Use of Arithmetic Operators. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

advertisement
  1. import java.util.Scanner;
  2. public class Arithmetic_Operators
  3. {
  4.    public static void main(String args[])
  5.    {   
  6.         Scanner s = new Scanner(System.in);
  7.         while(true)
  8.         {
  9.             System.out.println("");
  10.             System.out.println("Enter the two numbers to perform operations ");
  11.             System.out.print("Enter the first number : ");
  12.             int x = s.nextInt();
  13.             System.out.print("Enter the second number : ");
  14.             int y = s.nextInt();
  15.             System.out.println("Choose the operation you want to perform ");
  16.             System.out.println("Choose 1 for ADDITION");
  17.             System.out.println("Choose 2 for SUBTRACTION");
  18.             System.out.println("Choose 3 for MULTIPLICATION");
  19.             System.out.println("Choose 4 for DIVISION");
  20.             System.out.println("Choose 5 for MODULUS");
  21.             System.out.println("Choose 6 for EXIT");
  22.             int n = s.nextInt();
  23.             switch(n)
  24.             {
  25.                 case 1:
  26.                 int add;
  27.                 add = x + y;
  28.                 System.out.println("Result : "+add);
  29.                 break;
  30.  
  31.                 case 2:
  32.                 int sub;
  33.                 sub = x - y;
  34.                 System.out.println("Result : "+sub);
  35.                 break;
  36.  
  37.                 case 3:
  38.                 int mul;
  39.                 mul = x * y;
  40.                 System.out.println("Result : "+mul);
  41.                 break;
  42.  
  43.                 case 4:
  44.                 float div;
  45.                 div = (float) x / y;
  46.                 System.out.print("Result : "+div);
  47.                 break;
  48.  
  49.                 case 5:
  50.                 int mod;               
  51.                 mod = x % y;
  52.                 System.out.println("Result : "+mod);
  53.                 break;
  54.  
  55.                 case 6:
  56.                 System.exit(0);
  57.             }
  58.         }
  59.     }
  60. }

Output:

$ javac Arithmetic_Operators.java
$ java Arithmetic_Operators
 
Enter the two numbers to perform operations 
Enter the first number : 12
Enter the second number : 45
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
1
Result : 57
 
Enter the two numbers to perform operations 
Enter the first number : 56
Enter the second number : 42
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
3
Result : 2352
 
Enter the two numbers to perform operations 
Enter the first number : 45
Enter the second number : 56
Choose the operation you want to perform 
Choose 1 for ADDITION
Choose 2 for SUBTRACTION
Choose 3 for MULTIPLICATION
Choose 4 for DIVISION
Choose 5 for MODULUS
Choose 6 for EXIT
6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter