Knowee
Questions
Features
Study Tools

Write a C function, which returns the smallest odd number in a given array, and 0 if no such number is found.int smallest_odd(int a[] , int len);

Question

Write a C function, which returns the smallest odd number in a given array, and 0 if no such number is found.

int smallest_odd(int a[], int len);
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C function that does what you asked for:

#include <limits.h>

int smallest_odd(int a[], int len) {
    int smallest = INT_MAX;
    int found = 0;
    for(int i = 0; i < len; i++) {
        if(a[i] % 2 != 0 && a[i] < smallest) {
            smallest = a[i];
            found = 1;
        }
    }
    if(found) {
        return smallest;
    } else {
        return 0;
    }
}

This function works by iterating over each element in the array. If the element is odd (a[i] % 2 != 0) and it is smaller than the current smallest odd number found, it updates the smallest number. If no odd number is found after checking all elements, it returns 0.

This problem has been solved

Similar Questions

Given an array of integers, your task is to find the smallest and second smallest element in the array. If smallest and second smallest do not exist, print -1.

Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.

Write an HTML and JavaScript program which accepts N as input and print first Nodd numbers

Calculating the Product of Odd Integers) Write a program that calculates andprints the product of the odd integers from 1 to 15

Read a number from the user. Check whether it is odd or even using lambda function and print the output as either ODD or EVEN. Sample Input5Sample OutputODD

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.