In this tutorial you will learn about the Java Program to perform Arithmetic Operation using Method Overloading and its application with practical example.
Java Program to perform Arithmetic Operation using Method Overloading
In this tutorial, we will learn to create a Java program that will perform Arithmetic operations using Method Overloading in Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java Programming.
- Arithmetic operations in java programming.
Program to Description-:-
In today’s tutorial, we will create a program that will perform Arithmetic operations on the two numbers using the concepts method overloading. First, we will take input numbers from the user. We will use the arithmetic operators, in java, for calculations. Then we will print the output of different overloaded functions to the user. First, an addition function goes under the execution, then the overloaded method goes under execution.
With the help of this program, we can take input and perform the arithmetic operations.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the variables for the program. 2. Taking the input numbers from the user in number format. 3. Then we will perform the arithmetic operations on that numbers. 4. Printing the output to the user. 4. End program. |
Program to Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.*; class OverMeth{ //addition method of parameter a and b void addition(int a,int b){ int sum=a+b; System.out.println("Sum of two numbers is "+sum); } //addition method of parameter a, b and c void addition(int a,int b,int c){ int sum=a+b+c; System.out.println("Sum of three numbers is "+sum); } public static void main(String args[]){ OverloadingTest1 ovl=new OverloadingTest1(); ovl.addition(2,5,10); ovl.addition(15,10); } } |
Output:-
In the above program, we have first initialized the required variable.
- a= it will hold the number value for the program.
- b = it will hold the number value for the program.
- sum = it will hold the sum value of the numbers.
Adding with the first method of overloading.
Adding with the second method of overloading for three numbers.
Calling the functions and printing the output to the user.