This program prints the alternate elements in an array.
This is a C program which implements an array and prints the alternate elements of that array.
1. Create an array and define its elements according to the size.
2. Using for loop, access the elements of the array, but instead of incrementing the iterator by 1, increase it by 2, so as to get alternate indexes of the array.
Here is source code of the C Program to print the alternate elements in an array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to Print the Alternate Elements in an Array
*/
#include <stdio.h>
void main()
{
int array[10];
int i;
printf("enter the element of an array \n");
for (i = 0; i < 10; i++)
scanf("%d", &array[i]);
printf("Alternate elements of a given array \n");
for (i = 0; i < 10; i += 2)
printf( "%d\n", array[i]) ;
}
1. Declare an array of some fixed capacity, 10.
2. Define all its elements using scanf() function under for loop.
3. Now, run a for loop with an iterator i, incrementing it by 2 each time so as to get alternate alternate indexes.
4. Alternate indexes will give alternate elements of the array.
5. Print the alternative elements as well.
12 23 45 57 68 73 84 97 120 125 Alternate elements of a given array 12 45 68 84 120
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Check Computer Science Books
- Check C Books
- Apply for Computer Science Internship
- Apply for C Internship
- Practice BCA MCQs