The code below shows how to print an array using Index based for loop and iterat
ID: 3913267 • Letter: T
Question
The code below shows how to print an array using Index based for loop and iterator based for loop. Index based for loop: Int[] array={1,2,4,5,7}; for (int i = 0; i < array.length; i++) System.out.println(array[i]); Iterator based for loop: for (int datum : array) System.out.println(datum); Now write code generate 10 random numbers and assign it to an array. Now find the factorial of those 10 random numbers generated assign it to another array. Now print the output using Iterator based for loop and Index based for loop. The code below shows how to print an array using Index based for loop and iterator based for loop. Index based for loop: Int[] array={1,2,4,5,7}; for (int i = 0; i < array.length; i++) System.out.println(array[i]); Iterator based for loop: for (int datum : array) System.out.println(datum); Now write code generate 10 random numbers and assign it to an array. Now find the factorial of those 10 random numbers generated assign it to another array. Now print the output using Iterator based for loop and Index based for loop. The code below shows how to print an array using Index based for loop and iterator based for loop. Index based for loop: Int[] array={1,2,4,5,7}; for (int i = 0; i < array.length; i++) System.out.println(array[i]); Iterator based for loop: for (int datum : array) System.out.println(datum); Now write code generate 10 random numbers and assign it to an array. Now find the factorial of those 10 random numbers generated assign it to another array. Now print the output using Iterator based for loop and Index based for loop.Explanation / Answer
import java.util.Random;
class Main {
public static void main(String[] args) {
//given code in the question
int[] array={1,2,4,5,7};
for (int i = 0; i < array.length; i++) System.out.println(array[i]);
for (int datum : array) System.out.println(datum);
//generating random array
Random rand = new Random();
int[] array1=new int[10];
for(int i=0;i<10;i++){
array1[i] = rand.nextInt(9)+1;
}
int[] array2=new int[10];
for(int i=0;i<10;i++){
array2[i]=factorial(array1[i]);
}
//Printing the random array
System.out.println("Random array with indexed loop");
for (int i = 0; i < array1.length; i++) System.out.println(array1[i]);
//Priniting the factorial of the random array
System.out.println("Factorial array with iterated based loop");
for (int datum : array2) System.out.println(datum);
}
//function to calculate factorial
public static int factorial(int n){
if(n==1) return 1;
else return n*factorial(n-1);
}
}
sample output:
import java.util.Random;
class Main {
public static void main(String[] args) {
//given code in the question
int[] array={1,2,4,5,7};
for (int i = 0; i < array.length; i++) System.out.println(array[i]);
for (int datum : array) System.out.println(datum);
//generating random array
Random rand = new Random();
int[] array1=new int[10];
for(int i=0;i<10;i++){
array1[i] = rand.nextInt(9)+1;
}
int[] array2=new int[10];
for(int i=0;i<10;i++){
array2[i]=factorial(array1[i]);
}
//Printing the random array
System.out.println("Random array with indexed loop");
for (int i = 0; i < array1.length; i++) System.out.println(array1[i]);
//Priniting the factorial of the random array
System.out.println("Factorial array with iterated based loop");
for (int datum : array2) System.out.println(datum);
}
//function to calculate factorial
public static int factorial(int n){
if(n==1) return 1;
else return n*factorial(n-1);
}
}
sample output:
java version "1.8.0_31" Java(TM) SE Runtime Environment (build 1.8.0_31-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode) 1 2 4 5 7 1 2 4 5 7 Random array with indexed loop 4 2 8 4 4 8 3 8 1 9 Factorial array with iterated based loop 24 2 40320 24 24 40320 6 40320 1 362880