Q:

C program to print alternate numbers in an array

belongs to collection: C programs - Arrays and Pointers

0

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. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to print alternate numbers in an array - Source code

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program Count the Number of Occurrences of an El... >>
<< C program to find the number of elements in an arr...