In the previous article, we have seen Java Program to Multiply an Element to Every Other Elements of the Array
In this article we are going to see how we can divide an element to every other elements of the array except itself by using Java Language.
Java Program to Divide an Element to Every Other Elements of the Array
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to divide an element to every other elements of the array except itself.
Method-1: Java Program to Divide an Element to Every Element of the Array By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Enter the index of array element to find that specific element.
- This array element will be divided with other array elements.
- Iterate each element of the array and divide that specific array element with others elements except self.
Program:
public class Main { public static void main(String[] args) { //array initialized int arr[] = {10,20,30,40,50,60}; // creating new array with size of actual array int result[]= new int[arr.length]; System.out.println("Array length is : "+arr.length); //declaring array index of the specified number //which will be divided with other array elelments int num = 2; //if the entered index(means specified number) exists in the array //then only the specified array element can be divided with other array elements if(num<arr.length) { //iterating the array for(int i=0;i<arr.length;i++) { // checking condition // if array element is not the specified number // then it will enter into the if block // and the number will be divided with other elements except itself if(arr[i]!=arr[num]) { // dividing the specified array element with other array elements result[i] = arr[i]/arr[num]; } } } //assigning the specified number to the same index of result array //as the specified number will be same result[num]=arr[num]; //printing the result array System.out.println("New array after divide of array with a specific array element : "); for(int i=0;i<result.length;i++) { System.out.print(result[i]+" "); } } }
Output: Array length is : 6 New array after divide of array with a specific array element : 0 0 30 1 1 2
Method-2: Java Program to Divide an Element to Every Element of the Array By Dynamic Initialization of Array Elements
Approach:
- Take the array size as user input.
- Then take array elements as user input.
- Enter the index of array element to find that specific element.
- This array element will be divided with other array elements.
- Iterate each element of the array and divide that specific array element with others elements except self.
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // create scanner class object Scanner sc = new Scanner(System.in); // take input from user for array size System.out.print("Enter the size of array: "); int n = sc.nextInt(); // initialize array with size n int arr[] = new int[n]; // creating new array with size of actual array int result[]= new int[arr.length]; // take input from user for array elements System.out.print("Enter array elements: "); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println("Array length is : "+arr.length); //talking input of array index System.out.print("Enter index of the element to be divided : "); int num = sc.nextInt(); //if the entered index(means specified number) exists in the array //then only the specified array element can be divided with other array elements if(num<arr.length) { //iterating the array for(int i=0;i<arr.length;i++) { // checking condition // if array element is not the specified number // then it will enter into the if block // and the number will be divided with other elements except itself if(arr[i]!=arr[num]) { // dividing the specified array element with other array elements result[i] = arr[i]/arr[num]; } } } //assigning the specified number to the same index of result array //as the specified number will be same result[num]=arr[num]; //printing the result array System.out.println("New array after divide of array with a specific array element : "); for(int i=0;i<result.length;i++) { System.out.print(result[i]+" "); } } }
Output: Enter the size of array: 5 Enter array elements: 10 20 30 40 50 Array length is : 5 Enter index of the element to be divided : 0 New array after divide of array with a specific array element : 10 2 3 4 5
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.
Related Java Programs: