codescracker


c

C Program to Print Odd Numbers or Elements from given 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 some array elements and then print out all the odd numbers or elements from the given array. Here is the program:

// Write a program in C to receive any 10 array
// elements as input and then print all odd
// elements as output
// -----------codescracker.com-----------

#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

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

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). Here is the program:

// Write a program in C to read 10 values in an integer
// array and print all odd values/elements/numbers
// -----------codescracker.com-----------

#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

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

Let's modify the above program to allow the user to enter the size and elements for the array both. Here is the program:

// Write a program in C that allow user to enter
// array size and then array elements of that
// size to print out all the odd elements
// -----------codescracker.com-----------

#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 of above program:

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 the array of that size.


« C Tutorial C Examples »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test