*53*

# Smallest odd number

In this program, we need to find out the smallest odd number from the given integer number.

- We are provided with a random number, and from that number, we need to find the smallest odd digit present in it.
- For implementing it, we must know the basic concepts of array and loops.
- Whether you are implementing in C, C++, Java, or any other programming language, to solve this problem, you must have a basic understanding of the loops, and a linear data structure named an array.
- The array consists of a homogenous set of elements; for example, if a declared array with an int data type, that array must contain all integer types of values.
- We will use an array in this problem to store each digit of the given number.

Let us understand the concept of the above problem with the help of the below-mentioned examples:

** Example 1:** If we have given a number ‘ n ‘, n = 12345, we need to find the smallest odd number.

**n = 12345**

The number of odd numbers is: 1, 3, 5

But among the list of odd numbers, we require to find the smallest odd number,

**Therefore the output will be 1.**

** Example 2:** If we have given a number ‘ n ‘, n = 8745329, we need to find the smallest odd number.

**n = 874532**

The number of odd numbers is: 7, 5, 3, 9

But among the list of odd numbers, we require to find the smallest odd number,

**Therefore the output will be 3.**

** Example 3:** If we have given a number ‘ n ‘, n = 750, we need to find the smallest odd number.

**n = 750**

The number of odd numbers is: 7, 5

But among the list of odd numbers, we require to find the smallest odd number,

**Therefore the output will be 7.**

** Example 4:** If we have given a number ‘ n ‘, n = 220, we need to find the smallest odd number.

**n = 220**

The number of odd numbers is: none

But among the list of odd numbers, we require to find the smallest odd number,

**Therefore the output will be -1.**

**Here, -1 indicates that no smallest odd number is found from the given number, which means that all the numbers present are even.**

** Example 5:** If we have given a number ‘ n ‘, n = 97998, we need to find the smallest odd number.

**n = 97998**

The number of odd numbers is: 9, 7

But among the list of odd numbers, we require to find the smallest odd number,

**Therefore the output will be 7.**

** Example 6:** If we have given a number ‘ n ‘, n = 6620, we need to find the smallest odd number.

**n = 6620**

The number of odd numbers is: none

But among the list of odd numbers, we require to find the smallest odd number,

**Therefore the output will be -1.**

**Here, -1 indicates that no smallest odd number is found from the given number, which means that all the numbers present are even.**

With the help of the above examples, we have clearly understood what we need to do in this problem. Let us implement the above problem to find the smallest odd number using the approach mentioned below.

## Approach to find the smallest odd number

To implement this problem, we need to take the input from the user, separate all the digits from the given number and store all the digits in an array. After storing all the digits, we will compare each digit with another digit, and finally, we will find out the resulting smallest odd number.

The above approach is a very simple method to implement this problem and easy to understand even for the fresher indulging and learning programming skills. With the help of these programs, one can develop their programming skills very efficiently and effectively.

As we have already seen, the working of this problem, we all need to find the smallest odd number from the given integer value.

Let us see the way of implementation and algorithmic procedure of the above problem using the above approach:

## Algorithm to find the smallest odd number

**Step 1 –** Initially, take a random value, let say ‘ n ‘ from the user, so that the smallest odd digit can be detected from the given input.

**Step 2 –** After then we will count the number of digits present in the given number n; for example, if the input is 123678, we need to count 6 as the number of digits present in the given number.

**Step 3 –** For counting, we will take a variable named count, which counts the numbers of digits present in the given number.

**Step 4 –** Assign the input n into some other variable named ‘ i ‘, then continue the doo while loop, which calculates the number of digits present in the given number by incrementing the count variable by 1.

**Step 5 –** After counting, we will declare an array named as A of size count because we need to store all the digits present in the given number into the array separately to easily trace the digit and find out the odd and even digits.

**Step 6 –** For doing it, first, we will assign the value of n into some other variable, say ‘ i ‘, after assigning, we will run a loop from 0 to count – 1, to store the digits into an array. For accomplishing it, we will run a for a loop –

**Step 7 –** After this, we will find the odd number present in the array and compare it with a random, very large integer.

**Step 8 –** For doing it, we will initialize a variable named ‘ minodd ‘ and assign it with a very large integer value. We will compare each digit present in the array if the digit present in the array is smaller than the minodd. We will update the minodd variable and assign the digit to the minodd variable.

**Step 9 –** Similarly, we will check out for every digit present in the array and update the minodd variable accordingly. For doing so, we will use the loop given below:

**Step 10 –** At last, we will check if the minodd variable equals the assigned very large integer; if yes, we will update minodd with -1, which indicates that no smallest odd digit is found from the given number.

**Step 11 –** For implementing step 10, we will use the below-mentioned condition:

**Step 12 –** At last, for showing the result as the smallest odd digit, we will print the minodd, producing the desired output.

### Implementation of Problem to find the smallest odd number from the given number in C Programming Language

**The output of the above Program:**

### Implementation of Problem to find the smallest odd number from the given number in C++ Programming Language

**The output of the above Program:**

### Implementation of Problem to find the smallest odd number from the given number in Java Programming Language

**The output of the above Program:**

### Implementation of Problem to find the smallest odd number from the given number in Python Programming Language

**The output of the above Program:**

After executing the program successfully in a specific programming language, we will get the proper result, i.e., finding the smallest odd digit number from the given number.

Now let us analyze the running time and performance of the algorithm we are applying to sort the array in ascending order by finding the algorithm’s time complexity and space complexity.

### Time Complexity –

For finding the time complexity, we need to observe the algorithm and analyze the time taken by a particular line for execution. We can observe that we require traversing the number till the number of digits present in it. Hence time complexity will be –

T ( n ) = O ( n ) + C

Here, ‘ C ‘ is the constant

**T ( n ) = O ( n )**

Hence time complexity will be O ( n ), as we do the asymptotically rough idea that we require ‘ n ‘ time to solve the problem of ‘ n ‘ elements.

### Space Complexity –

To find space complexity, we need to observe the program and analyze the space required to store all the variables; as we notice the program, we will observe that no extra space is required to store the elements.

**S ( n ) = O ( 1 )**

Hence the space complexity will be constant.