C Program to Print Odd Numbers in Array

In this tutorial, we will learn about how to create a program in C that will ask from 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 under Code::Blocks IDE, therefore after successful build and run, here is the first snapshot of the sample run:

c print odd values from array

Provide any 10 array elements and then press 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

  • Receive any 10 numbers as 10 array elements
  • Create for loop starts from 0 to 9, as indexing starts from 0
  • Inside the for loop, check whether the element/number present at current array index is an odd number or not
  • If it is an odd number, then print it out, otherwise continue to check for the next odd array element
  • In this way, all the odd number gets printed on the output screen

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 original array (provided by 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 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 ENTER key to see the following output:

print odd array elements c

Program Explained

  • Receive any 10 array elements
  • Create a for loop starts from 0 to 9
  • Check whether the current element is an odd number or not
  • If it is an odd number, then place this array element inside another array say b[]
  • Indexing starts in array b[] (where all the odd number of original array will gets placed) starts with 0
  • Therefore the variable provided here say j initialized with 0 and then incremented with one each time, when the odd number from original array gets placed inside the second array say b[]
  • Also increment the value of a variable say count (responsible for the length of second array say b[]
  • After checking each and every element of original array, and placing the element when it will be an odd number inside the second array
  • Come out from this for loop
  • Create another for loop, and print all the value of second array say b[]
  • Here the array b[] contains all the odd array elements present inside the original array provided by user
  • In this way, we will see all the odd array elements on the output screen

Allow User to Modify Array Size

Let's modify the above program to allow the user to define the array size too 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 used in above program is almost same, except we have used size variable (that holds the size for array provided by user at run-time) in place of 10, that is here user has allowed to enter the size for the array and then elements for same array.

C Online Test


« Previous Program Next Program »