C Program to Print Array Elements present at Even Position

In this tutorial, we will learn about how to create a program in C that will ask from user to enter array elements to print all the array elements present at even index number or even positions. Here is the program.

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[10], i;
    printf("Enter any 10 array elements: ");
    for(i=0; i<10; i++)
        scanf("%d", &arr[i]);
    printf("\nValues stored at even position are:\n");
    for(i=0; i<10; i++)
    {
        if(i%2==0)
            printf("%d ", arr[i]);
    }
    getch();
    return 0;
}

As the program was written under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run:

c print value stored at even position

As we all knows that the indexing in array starts form 0, therefore here is the second snapshot of the sample run. User has to provide any 10 numbers as 10 array elements for the given array and press ENTER key to see the following output:

print value stored at even position c

Here are the list of some main steps used in above program:

  • Receive any 10 numbers as array elements
  • Create a for loop stars from 0th number (array element) to 9th number (array element)
  • Inside the for loop, check whether the index number is an even number or not
  • If it is an even number, then print the value present at that even index number
  • In this way, we will see all the array elements present at even index number on output screen after running the program

Allow User to Define Array Size

Here is the modified version of the above program. In this program, we have allowed the user to enter the size of the array at run-time:

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[100], i, limit;
    printf("How many elements you want to store inside the array: ");
    scanf("%d", &limit);
    printf("Enter any %d array elements: ", limit);
    for(i=0; i<limit; i++)
        scanf("%d", &arr[i]);
    printf("\n\nValues stored at even position (with array and its index) are:\n");
    for(i=0; i<limit; i++)
    {
        if(i%2==0)
            printf("arr[%d] = %d\n", i, arr[i]);
    }
    getch();
    return 0;
}

Here is the final snapshot of sample run:

print value at even position c

Here are some of the main steps used in above program:

  • Receive the size for the array say 20
  • Now receive any 20 numbers as 20 array elements
  • And then follow all the steps as told in previous program
  • That is to find the even index number one by one to print all the values stored at even index numbers

C Online Test


« Previous Program Next Program »



Like/Share Us on Facebook 😋