adplus-dvertising

C program to print alternate numbers in an array

frame-decoration

C program to print alternate numbers in an array

Problem Description

The logic to print the alternate elements in an array is very simple. We use the for loop to iterate through the array starting with the first element. Instead of increment the for loop variable by 1, increment it by 2.

C program to print alternate numbers in an array - Source code
     
                
  /* C Program to Print the Alternate Elements in an Array
 */

#include <stdio.h>

int main()
{
    int array[12];
    int i, j, temp;
    printf("enter the element of the array \n");

    for (i = 0; i < 12; i++)
        scanf("%d", &array[i]);

    printf("Alternate elements of the entered array \n");
    for (i = 0; i < 12; i += 2)
        printf( "%d\n", array[i]) ;
}

     
      

Program Output

enter the element of the array
1
2
3
4
5
6
7
8
9
0
11
12
Alternate elements of the entered array
1
3
5
7
9
11

Program Explanation

1. First, user is asked to enter the elements of the array. Here Array of 12 numbers is declared, however you can choose this number of your choice.

2. The elements are stored in the array at each indices using the for loop.

3. In order to print the alternate numbers of the array, start the loop with 0 and increment it by 2 with every iteration.