C Program to Print Odd Numbers in an Array

In this tutorial, we will learn how to create a program in C that will ask the user to enter array elements and then print out all the odd numbers or elements from the given array. 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("\nOdd Array elements are:\n");
    for(i=0; i<10; i++)
    {
        if(arr[i]%2!=0)
        {
            printf("%d ", arr[i]);
        }
    }
    getch();
    return 0;
}

As the above program was written in the Code::Blocks IDE, here is the first snapshot of the sample run:

c print odd values from array

Provide any 10 array elements and then press the ENTER key to see the output that will list out all the odd elements from the list of given 10 array elements, as shown here in the second snapshot:

print odd array values as output c

Program Explained

Copy odd numbers to another array

Now let's modify the above program and create another program that will create another array containing all odd array elements of the original array (provided by the user):

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[10], i, b[10], j=0, count=0;
    printf("Enter any 10 array elements: ");
    for(i=0; i<10; i++)
        scanf("%d", &arr[i]);
    for(i=0; i<10; i++)
    {
        if(arr[i]%2!=0)
        {
            b[j] = arr[i];
            count++;
            j++;
        }
    }
    printf("\nOdd elements are:\n");
    for(i=0; i<count; i++)
    {
        if(i==(count-1))
            printf("%d", b[i]);
        else
            printf("%d, ", b[i]);
    }
    getch();
    return 0;
}

After a successful build and run, here is the first snapshot of the sample run:

c program print odd array elements

Provide any 10 array elements and then press the ENTER key to see the following output:

print odd array elements c

Program Explained

Allow the user to change the size of the array

Let's modify the above program to allow the user to define the array size along with its elements:

#include<stdio.h>
#include<conio.h>
int main()
{
    int arr[10], i, b[10], j=0, count=0, size;
    printf("Enter array size: ");
    scanf("%d", &size);
    printf("Enter any %d array elements: ", size);
    for(i=0; i<size; i++)
        scanf("%d", &arr[i]);
    for(i=0; i<size; i++)
    {
        if(arr[i]%2!=0)
        {
            b[j] = arr[i];
            count++;
            j++;
        }
    }
    printf("\nOdd elements are:\n");
    for(i=0; i<count; i++)
    {
        if(i==(count-1))
            printf("%d", b[i]);
        else
            printf("%d, ", b[i]);
    }
    getch();
    return 0;
}

Here is the final snapshot of the sample run:

c print odd array elements

The logic in the preceding program is nearly identical, with the exception that we have used the "size" variable (which holds the size for the array provided by the user at run-time) instead of 10, implying that the user is allowed to enter the size for the array and then elements for the same array.

C Quiz


« Previous Program Next Program »


Liked this post? Share it!