Q:

C program to print alternate elements of the array

0

C program to print alternate elements of the array

All Answers

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

Here, we will create an array of integers then print alternate elements of an array on the console screen.

Program:

The source code to print alternate elements of the array is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to print alternate elements of array.

#include <stdio.h>

int main()
{
    int arr[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 10 };
    int i = 0;

    printf("Alternate elements of array: ");
    for (i = 0; i < 10; i = i + 2)
        printf("\n\tarr[%d] is: %d", i, arr[i]);

    printf("\n");
    return 0;
}

Output:

Alternate elements of array: 
	arr[0] is: 10
	arr[2] is: 30
	arr[4] is: 50
	arr[6] is: 70
	arr[8] is: 90

Explanation:

Here, we created an array arr with 10 elements, and then we increased the value of the counter variable i by 2 in the for loop to print alternate elements of an array on the console screen.

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

total answers (1)

One Dimensional Array Programs / Examples in C programming language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to print the non-repeated elements of an... >>
<< C program to access array element out of bounds...