Skip to content
Related Articles

Related Articles

Improve Article

Smallest odd number with N digits

  • Last Updated : 25 Mar, 2021
Geek Week

Given a number N. The task is to find the smallest N digit ODD number.
Examples: 
 

Input: N = 1
Output: 1

Input: N = 3
Output: 101 

 

Approach: There can be two cases depending on the value of N. 
Case 1 : If N = 1 then answer will be 1. 
Case 2 : If N != 1 then answer will be (10^(n-1)) + 1 because the series of the smallest odd numbers will go on like: 1, 11, 101, 1001, 10001, 100001, ….
Below is the implementation of the above approach:
 

C++




// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return smallest odd
// with n digits
int smallestOdd(int n)
{
    if (n == 1)
        return 1;
 
    return pow(10, n - 1) + 1;
}
 
// Driver Code
int main()
{
    int n = 4;
    cout << smallestOdd(n);
 
    return 0;
}

Java




// Java implementation of the approach
class Solution {
 
    // Function to return smallest odd with n digits
    static int smallestOdd(int n)
    {
        if (n == 1)
            return 0;
        return Math.pow(10, n - 1) + 1;
    }
 
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
 
        System.out.println(smallestOdd(n));
    }
}

Python3




# Python3 implementation of the approach
 
# Function to return smallest even
# number with n digits
def smallestOdd(n) :
 
    if (n == 1):
        return 1
    return pow(10, n - 1) + 1
 
# Driver Code
n = 4
print(smallestOdd(n))
 
# This code is contributed by ihritik.

C#




// C# implementation of the approach
using System;
class Solution {
 
    // Function to return smallest odd with n digits
    static int smallestOdd(int n)
    {
        if (n == 1)
            return 0;
 
        return Math.pow(10, n - 1) + 1;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
 
        Console.Write(smallestOdd(n));
    }
}

PHP




<?php
// PHP implementation of the approach
 
// Function to return smallest even
// number with n digits
function smallestOdd($n)
{
    if ($n == 1)
        return 1;
    return pow(10, $n - 1) + 1;
}
 
// Driver Code
$n = 4;
echo smallestOdd($n);
 
// This code is contributed by ihritik
?>

Javascript




<script>
 
  // Javascript implementation of the above approach
 
  // Function to return smallest odd
  // with n digits
  function smallestOdd(n) {
    if (n == 1)
      return 1;
 
    return Math.pow(10, n - 1) + 1;
  }
 
  // Driver Code
  var n = 4;
  document.write(smallestOdd(n));
   
  // This code is contributed by rrrtnx.
</script>
Output: 
1001

 

Time Complexity: O(1).
 

Attention reader! All those who say programming isn’t for kids, just haven’t met the right mentors yet. Join the  Demo Class for First Step to Coding Coursespecifically designed for students of class 8 to 12. 

The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.




My Personal Notes arrow_drop_up
Recommended Articles
Page :