3 different C programs to print alternate numbers of array

C program to print alternate numbers of a user input array:

In this tutorial, we will learn how to print alternate numbers of an array. The user will enter the total count of numbers to add to the array. The program will read all the numbers and print the alternate numbers of the array.

This program will give you an idea of loops in C. You will learn how to use a for loop, while loop, and do...while loop with examples.

Method 1: Print the alternate numbers of an array with a for loop:

The following program uses a for loop to print the alternate numbers of a given array:

#include <stdio.h>

int main()
{
    //1
    int i;
    int size;

    //2
    printf("How many numbers you want to enter: ");
    scanf("%d", &size);

    //3
    int array[size];

    //4
    for (i = 0; i < size; i++)
    {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &array[i]);
    }

    //5
    printf("Alternate elements of the input array: \n");
    for (i = 0; i < size; i += 2)
    {
        printf("%d ", array[i]);
    }
}

Download it on GitHub

Explanation:

The commented numbers in the above program denote the step number below :

  1. Create one variable i to use in the loop. Also, create another variable size to assign the size of the array.
  2. Read the total count of numbers from the user and assign this value to the variable size.
  3. Create one array with a length equal to the user-given size value.
  4. Run one for loop to read the contents for the array. Read each element one by one and add these to the array.
  5. Use one more for loop and print the alternate elements of the array. In this loop, we are incrementing the value of i by 2 at the end of each iteration, i += 2. So, this loop will iterate over the alternate numbers of the array.

Print alternate numbers of an array in C

Sample Output :

If you run this program, it will print outputs as below:

How many numbers you want to enter: 10
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Enter number 8: 8
Enter number 9: 9
Enter number 10: 10
Alternate elements of the input array:
1
3
5
7
9
How many numbers you want to enter: 10
Enter number 1: 10
Enter number 2: 9
Enter number 3: 8
Enter number 4: 7
Enter number 5: 6
Enter number 6: 5
Enter number 7: 4
Enter number 8: 3
Enter number 9: 2
Enter number 10: 1
Alternate elements of the input array:
10
8
6
4
2

Method 2: By using a while loop:

We can also use a while loop instead of a for loop to write the same program. The syntax of the while loop is:

while(condition){
    // body
}

It will keep running the body of the loop until the condition is True. The following example uses a while loop to print the alternate numbers of the array. It also uses a while loop to read the values from the user:

#include <stdio.h>

int main()
{
    int i;
    int size;

    printf("How many numbers you want to enter: ");
    scanf("%d", &size);

    int array[size];

    i = 0;
    while (i < size)
    {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &array[i]);
        i++;
    }

    printf("Alternate elements of the input array: \n");
    i = 0;
    while (i < size)
    {
        printf("%d ", array[i]);
        i += 2;
    }
}

Download it on GitHub

It will print similar output.

How many numbers you want to enter: 5
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Alternate elements of the input array: 
1 3 5

Method 3: By using a do...while loop:

The do...while loop is similar to a while loop. It runs the body of the loop before it verifies the condition. The syntax of a do...while loop is:

do{
    // body
}while(condition);

It checks for the condition after the code in the body of the loop is executed. So, even if the condition is false, it will run the body of the loop once.

The following program shows how we can use do...while loops to read the inputs and iterate over the array:

#include <stdio.h>

int main()
{
    int i;
    int size;

    printf("How many numbers you want to enter: ");
    scanf("%d", &size);

    if (size <= 0)
    {
        printf("Please enter a valid input!!");
        return 0;
    }
    int array[size];

    i = 0;
    do
    {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &array[i]);
        i++;
    } while (i < size);

    printf("Alternate elements of the input array: \n");
    i = 0;
    do
    {
        printf("%d ", array[i]);
        i += 2;
    } while (i < size);
}

Download it on GitHub

It will print similar results. We added a condition to check if the size is valid or not. If it is smaller than 0, it prints a message to enter a valid input and returns from the program.

if (size <= 0)
{
    printf("Please enter a valid input!!");
    return 0;
}

This check can be added to other examples as well.

You might also like: