Smallest odd number with N digits
Last Updated :
22 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int smallestOdd( int n)
{
if (n == 1)
return 1;
return pow (10, n - 1) + 1;
}
int main()
{
int n = 4;
cout << smallestOdd(n);
return 0;
}
|
Java
class Solution {
static int smallestOdd( int n)
{
if (n == 1 )
return 0 ;
return Math.pow( 10 , n - 1 ) + 1 ;
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(smallestOdd(n));
}
}
|
Python3
def smallestOdd(n) :
if (n = = 1 ):
return 1
return pow ( 10 , n - 1 ) + 1
n = 4
print (smallestOdd(n))
|
C#
using System;
class Solution {
static int smallestOdd( int n)
{
if (n == 1)
return 0;
return Math.pow(10, n - 1) + 1;
}
public static void Main()
{
int n = 4;
Console.Write(smallestOdd(n));
}
}
|
PHP
<?php
function smallestOdd( $n )
{
if ( $n == 1)
return 1;
return pow(10, $n - 1) + 1;
}
$n = 4;
echo smallestOdd( $n );
?>
|
Javascript
<script>
function smallestOdd(n) {
if (n == 1)
return 1;
return Math.pow(10, n - 1) + 1;
}
var n = 4;
document.write(smallestOdd(n));
</script>
|
Time Complexity: O(log n).
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...