I get an exception in my second print line.
int num[] = {50,20,45,82,25,63};
System.out.print("Given number : ");
for(int d:num){
System.out.print(" " + num[d]);
}
The console output is
Given number : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50
Why doesn’t d take all the array elements but only 50?
In the
for(int d:num)loop every item is represented bydnotnum[d]So, here is how it should be done.
A simple dry run will show you where you went wrong.
However, if your attempt was to use indexing, then a simple trick below will do the trick
But I honestly believe, this is not the correct solution to the problem.