codescracker
c

C Program to Print Odd Numbers in Array



« C Tutorial C Examples »

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

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

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


« C Tutorial C Examples »



© Copyright 2021. All Rights Reserved.

CodesCracker