Home > Net >  sorting odd number in a new array, output problem
sorting odd number in a new array, output problem

Time:12-02

to store odd numbers from a user inputed array(arr) in a new array(arr1), then printing. run and see output first

#include <stdio.h>
int main()
{
    int i;
    int arr1[100];
    int arr[5] = {2, 3, 8, 9, 11};
    for (i = 0; i <= 5; i  )
    {
        for (int x = 1; x < 12; x  )
        {
            if (x == arr[i])
            {
                arr1[i] = arr[i];
            }
            x  = 1;
        }
    }
    for (i = 0; i < 5; i  )
    {
        printf("%d\n", arr1[i]);
    }

    return 0;
}

CodePudding user response:

You can check if it is divisible by two using modulo operator %. If it is not divisible by 2 this should get 1. This should work:

#include <stdio.h>
int main()
{
    int i;
    int arr1[100];
    int arr[5] = {2, 3, 8, 9, 11};
    int j=0;
    for (int i=0; i < 5; i  ) {
        if (arr[i]%2 == 1) { //is odd
            arr1[j  ] = arr[i];
        }

    }
    
    return 0;
}

  • Related