Java Program to Perform Arithmetic Operations Using Methods | Switch Case
Java program to perform arithmetic operations using methods and switch case.
In this article, you will learn how to make a java program to perform arithmetic operations using methods and switch case.
Example
Enter any two positive integer numbers:
5
8
SUM 5 + 8 = 13
DIFFERENCE 5 - 8 = -3
PRODUCT 5 * 8 = 40
QUOTIENT 5 / 8 = 0.0
MODULUS 5 % 8 = 5
You should have the knowledge of the following topics in Java programming to understand this program.
- Java Arithmetic operators
- Java
java.util.Scanner
package - Java
main()
method - Java
System.out.println()
function - Java Methods
- Java Switch Case
There we will perform these arithmetic operations like Sum, Difference, Multiplication, Division, and Modulus.
In this article, we solve this problem in three methods:
Source Code
// Java Program to Perform Arithmetic Operations
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter any two positive integer numbers:");
// Reading data using readLine
int p = in.nextInt();
int q = in.nextInt();
int sum, sub, mul, mod;
float div;
sum = p + q;
sub = p - q;
mul = p * q;
div = p / q;
mod = p % q;
System.out.println("\nSUM " + p + " + " + q + " = " + sum);
System.out.println("DIFFERENCE " + p + " - " + q + " = " + sub);
System.out.println("PRODUCT " + p + " * " + q + " = " + mul);
System.out.println("QUOTIENT " + p + " / " + q + " = " + div);
System.out.println("MODULUS " + p + " % " + q + " = " + mod);
}
}
Output
Enter any two positive integer numbers:
5
8
SUM 5 + 8 = 13
DIFFERENCE 5 - 8 = -3
PRODUCT 5 * 8 = 40
QUOTIENT 5 / 8 = 0.0
MODULUS 5 % 8 = 5
Explanation
In this given program, we have taken inputs 5
and 8
from the user then we applied arithmetic operations on these integer numbers.
Then this will be returned the output values of this program.
Java Program to Perform Arithmetic Operations using Methods
// Java Program to Perform Arithmetic Operations using Methods
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter any two positive integer numbers:");
// Reading data using readLine
int p = in.nextInt();
int q = in.nextInt();
System.out.println("\nSUM " + p + " + " + q + " = " + Sum(p, q));
System.out.println("DIFFERENCE " + p + " - " + q + " = " + Sub(p, q));
System.out.println("PRODUCT " + p + " * " + q + " = " + Mul(p, q));
System.out.println("QUOTIENT " + p + " / " + q + " = " + Div(p, q));
System.out.println("MODULUS " + p + " % " + q + " = " + Mod(p, q));
}
// To make sum of two numbers
public static int Sum(int x, int y) {
return x + y;
}
// To make subtraction of two numbers
public static int Sub(int x, int y) {
return x - y;
}
// To make multilication of two numbers
public static int Mul(int x, int y) {
return x * y;
}
// To make division of two numbers
public static float Div(int x, int y) {
return x / y;
}
// To make modulus of two numbers
public static int Mod(int x, int y) {
return x % y;
}
}
Java Program to Perform Arithmetic Operations using Switch Case
// Java Program to Perform Arithmetic Operations using Switch Case
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter any two positive integer numbers:");
// Reading data using readLine
int p = in.nextInt();
int q = in.nextInt();
System.out.println("\nEnter one option to make calculation::");
System.out.println("Option 1 :: To make sum");
System.out.println("Option 2 :: To make subtraction");
System.out.println("Option 3 :: To make multiplication");
System.out.println("Option 4 :: To make division");
System.out.println("Option 5 :: To make modulus\n");
int c = in.nextInt();
float Calc = 0;
switch(c) {
// To make sum of two numbers
case 1:
Calc = p + q;
System.out.println("\nSUM " + p + " + " + q + " = " + Calc);
break;
// To make subtraction of two numbers
case 2:
Calc = p - q;
System.out.println("\nDIFFERENCE " + p + " - " + q + " = " + Calc);
break;
// To make multilication of two numbers
case 3:
Calc = p * q;
System.out.println("\nPRODUCT " + p + " * " + q + " = " + Calc);
break;
// To make division of two numbers
case 4:
Calc = p / q;
System.out.println("\nQUOTIENT " + p + " / " + q + " = " + Calc);
break;
// To make modulus of two numbers
case 5:
Calc = p % q;
System.out.println("\nMODULUS " + p + " % " + q + " = " + Calc);
break;
default:
System.out.println("\nYou choosed the wrong option!");
}
}
}
Output
Enter any two positive integer numbers:
5
8
Enter one option to make calculation::
Option 1 :: To make sum
Option 2 :: To make subtraction
Option 3 :: To make multiplication
Option 4 :: To make division
Option 5 :: To make modulus
1
SUM 5 + 8 = 13.0
You should also read these things
Simple Interest Program in Java using Methods | Inheritance
Java Program to Check Even or Odd Number using If-Else Statement | Ternary Operator | Function
Find Factors of a Number in Java using While loop | For | Recursion | Functions
Java Program to Enter Marks of Five Subjects and Calculate Total, Percentage, and Grade