DEV Community

Cover image for Print Alternate elements in array...
Roshan Shambharkar
Roshan Shambharkar

Posted on

Print Alternate elements in array...

The Idea is simple to print the Alternate elements in array for that as beginner who have to hands on some traverse an array using for loop so if you have that idea how to traverse an array then it is easy to you find alternates elements in array if you dont have knowledge about how to traverse an array then here is some resources where you can learn basic of programming LINK

Now come for solutions i explained solutions in some steps follow this steps to understand the idea
Step 1 Using For Loop Traverse an array throughout its size or leneght

Step 2 Increment array by two number using ** i = i + 2**

thats all you need to know for print alernate elements in array

Now Lets Check The code, and you will get complete clearance about the logic

class Test {
Public static void main(String args[]) {
int arr[] = {1, 2, 3, 4, 5};
int sizeOfArray = arr.length;
for(int i = 0; i<sizeOfArray; i = i + 2) {
System.out.print(arr[i]+" ");
}
}
}

the output will : 1, 3, 5

Top comments (0)