How to write a Java Program to Perform Arithmetic Operations on Array using For Loop, While Loop, and Functions with an example.
Java Program to Perform Arithmetic Operations on Array using For Loop
This Java program allows the user to enter the size and the One Dimensional Array elements. Next, this Java program performs the Arithmetic Operations such as Addition, Subtraction, Multiplication, and Division using those two arrays.
// Java Program to Perform Arithmetic Operations on Array import java.util.Scanner; public class ArrayArithmeticOpert1 { private static Scanner sc; public static void main(String[] args) { int Size, i; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements in an array : "); Size = sc.nextInt(); int [] a = new int[Size]; int [] b = new int[Size]; int [] Addition = new int[Size]; int [] Subtraction = new int[Size]; int [] Multiplication = new int[Size]; int [] Division = new int[Size]; float [] Module = new float[Size]; System.out.print(" Please Enter " + Size + " elements of an Array : "); for (i = 0; i < Size; i++) { a[i] = sc.nextInt(); } System.out.print(" Please Enter " + Size + " elements of an Array : "); for (i = 0; i < Size; i++) { b[i] = sc.nextInt(); } for(i = 0; i < Size; i ++) { Addition [i]= a[i] + b[i]; Subtraction [i]= a[i] - b[i]; Multiplication [i]= a[i] * b[i]; Division [i] = a[i] / b[i]; Module [i] = a[i] % b[i]; } System.out.println("\nAdd\t Sub\t Multi\t Div\t Mod"); for (i = 0; i < Size; i++) { System.out.print(Addition[i] + " \t "); System.out.print(Subtraction[i] + " \t "); System.out.print(Multiplication[i] + " \t "); System.out.print(Division[i] + " \t "); System.out.print(Module[i]+ " \t "); System.out.print("\n"); } } }
First, We declared 6 arrays of user-specified size. And they are a, b, Addition, Subtraction, Multiplication, and Module of integer type and 1 array Division with the float data type.
The below For Loop iterate each cell present in a[3] array. Condition inside the for loops (i < Size)) ensure the compiler not exceed the array size. The statement inside the for loop stores the user entered values as array element such as a[0], a[1], a[2].
for (i = 0; i < Size; i++) { a[i] = sc.nextInt(); }
Next, we used another Java For Loop to assign values to b Array.
In the next line, We have one more for loop to iterate each element and calculate the Addition, Subtraction, Multiplication, Division, and Module of 2 arrays. From the above Arithmetic Operations screenshot
for(i = 0; i < Size; i++) { Addition [i] = a[i] + b[i]; Subtraction [i] = a[i] - b[i]; Multiplication [i] = a[i] * b[i]; Division [i] = a[i] / b[i]; Module [i] = a[i] % b[i]; }
The user inserted values for Java Program to Perform Arithmetic Operations on Array are
a[3] = {15, 35, 55}} and
b[3] = {10, 20, 35}}
First Iteration: for (i = 0; 0 < 3; 0++)
The value of i = 0, and the condition (i < 3) is True. So, it starts executing the statements inside the loop until the condition fails.
Addition[i] = a[i] + b[i] => a[0] + b[0];
Addition[0] = 15 + 10 = 25
Subtraction[0]= a[0] – b[0]
Subtraction [0] = 15 – 10 = 5
Multiplication[0] = a[0] * b[0]
Multiplication [0] = 15 * 10 = 150
Division[0] = a[0] / b[0]
Division [0] = 15 / 10 = 1
Module[0] = a[0] % b[0]
Module[0] = 15 % 10 = 5
Second Iteration: for (i = 1; 1 < 3; 1++)
The value of i is 1, and the condition (i < 3) is True.
Addition[1] = a[1] + b[1];
Addition[1] = 35 + 20 = 55
Subtraction[1] = 35 – 20 = 15
Multiplication[1] = 35 * 20 = 700
Division[1] = 35 / 20 = 1
Module[1] = 35 % 20 = 15
Third Iteration: for (i = 2; 2 < 3; 2++)
Addition[2] = a[2] + b[2];
Addition[2] = 55 + 35 = 90
Subtraction[2] = 55 – 35 = 20
Multiplication[2] = 55 * 35 = 1925
Division[2] = 55 / 35 = 1
Module[2] = 55 % 35 = 20
After the increment, the value of columns = 3, and the condition (i < 3) fail. So, it exits from the loop.
Next for loops,
for (i = 0; i < Size; i++) { System.out.print(Addition[i] + " \t "); System.out.print(Subtraction[i] + " \t "); System.out.print(Multiplication[i] + " \t "); System.out.print(Division[i] + " \t "); System.out.print(Module[i]+ " \t "); System.out.print("\n"); }
The final output of the Addition Array is:
Addition [3] = {25, 55, 90};
The final output of the Subtraction Array is:
Subtraction [3] = {5, 15, 20};
The final output of the Multiplication Array is:
Multiplication [3] = {150, 700, 1925};
The final output of the Division array is:
Division [3] = {1.00, 1.00, 1.00};
The final output of the Module Array is:
Module [2][3] = { {5, 15, 20};
Java Program to Perform Arithmetic Operations on Array using Functions
This Java program is the same as the first example. But we used a separate method to perform Arithmetic operations on one-dimensional array elements.
// Java Program to Perform Arithmetic Operations on Array import java.util.Scanner; public class ArrayArithmeticOpert3 { private static Scanner sc; public static void main(String[] args) { int Size, i = 0, j = 0; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements in an array : "); Size = sc.nextInt(); int [] a = new int[Size]; int [] b = new int[Size]; System.out.print(" Please Enter " + Size + " elements of an Array : "); while(i < Size) { a[i] = sc.nextInt(); i++; } System.out.print(" Please Enter " + Size + " elements of an Array : "); while(j < Size ) { b[j] = sc.nextInt(); j++; } ArithmeticOpertioons(a, b, Size); } public static void ArithmeticOpertioons(int a[], int b[], int Size) { int i = 0, j = 0; int [] Addition = new int[Size]; int [] Subtraction = new int[Size]; int [] Multiplication = new int[Size]; int [] Division = new int[Size]; float [] Module = new float[Size]; while(i < Size) { Addition [i]= a[i] + b[i]; Subtraction [i]= a[i] - b[i]; Multiplication [i]= a[i] * b[i]; Division [i] = a[i] / b[i]; Module [i] = a[i] % b[i]; i++; } System.out.println("\nAdd\t Sub\t Multi\t Div\t Mod"); while(j < Size ) { System.out.print(Addition[j] + " \t "); System.out.print(Subtraction[j] + " \t "); System.out.print(Multiplication[j] + " \t "); System.out.print(Division[j] + " \t "); System.out.print(Module[j]+ " \t "); System.out.print("\n"); j++; } } }
Java Array Arithmetic Operations output
Please Enter Number of elements in an array : 5
Please Enter 5 elements of an Array : 10 20 30 40 50
Please Enter 5 elements of an Array : 2 15 28 14 75
Add Sub Multi Div Mod
12 8 20 5 0.0
35 5 300 1 5.0
58 2 840 1 2.0
54 26 560 2 12.0
125 -25 3750 0 50.0
Java Program to Perform Arithmetic Operations on Array using While Loop
This program is the same as above, but we used Java While Loop to perform arithmetic operations on the array elements
// Java Program to Perform Arithmetic Operations on Array import java.util.Scanner; public class ArrayArithmeticOpert2 { private static Scanner sc; public static void main(String[] args) { int Size, i; sc = new Scanner(System.in); System.out.print(" Please Enter Number of elements in an array : "); Size = sc.nextInt(); int [] a = new int[Size]; int [] b = new int[Size]; System.out.print(" Please Enter " + Size + " elements of an Array : "); for (i = 0; i < Size; i++) { a[i] = sc.nextInt(); } System.out.print(" Please Enter " + Size + " elements of an Array : "); for (i = 0; i < Size; i++) { b[i] = sc.nextInt(); } ArithmeticOpertioons(a, b, Size); } public static void ArithmeticOpertioons(int a[], int b[], int Size) { int i; int [] Addition = new int[Size]; int [] Subtraction = new int[Size]; int [] Multiplication = new int[Size]; int [] Division = new int[Size]; float [] Module = new float[Size]; for(i = 0; i < Size; i ++) { Addition [i]= a[i] + b[i]; Subtraction [i]= a[i] - b[i]; Multiplication [i]= a[i] * b[i]; Division [i] = a[i] / b[i]; Module [i] = a[i] % b[i]; } System.out.println("\nAdd\t Sub\t Multi\t Div\t Mod"); for (i = 0; i < Size; i++) { System.out.print(Addition[i] + " \t "); System.out.print(Subtraction[i] + " \t "); System.out.print(Multiplication[i] + " \t "); System.out.print(Division[i] + " \t "); System.out.print(Module[i]+ " \t "); System.out.print("\n"); } } }
Java Array Arithmetic Operations output
Please Enter Number of elements in an array : 4
Please Enter 4 elements of an Array : 12 48 89 63
Please Enter 4 elements of an Array : 89 16 128 43
Add Sub Multi Div Mod
101 -77 1068 0 12.0
64 32 768 3 0.0
217 -39 11392 0 89.0
106 20 2709 1 20.0