Smallest Even number with N digits
Last Updated :
22 Jun, 2022
Given a number N, the task is to find the smallest Even number with N digits.
Examples:
Input: N = 1
Output: 0
Input: N = 2
Output: 10
Approach:
Case 1 : If N = 1 then answer will be 0.
Case 2 : if N != 1 then answer will be (10^(N-1)) because the series of smallest even numbers will go on like, 0, 10, 100, 1000, 10000, 100000, …..
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int smallestEven( int n)
{
if (n == 1)
return 0;
return pow (10, n - 1);
}
int main()
{
int n = 4;
cout << smallestEven(n);
return 0;
}
|
Java
class Solution {
static int smallestEven( int n)
{
if (n == 1 )
return 0 ;
return Math.pow( 10 , n - 1 );
}
public static void main(String args[])
{
int n = 4 ;
System.out.println(smallestEven(n));
}
}
|
Python3
def smallestEven(n) :
if (n = = 1 ):
return 0
return pow ( 10 , n - 1 )
n = 4
print (smallestEven(n))
|
C#
using System;
class Solution {
static int smallestEven( int n)
{
if (n == 1)
return 0;
return Math.pow(10, n - 1);
}
public static void Main()
{
int n = 4;
Console.Write(smallestEven(n));
}
}
|
PHP
<?php
function smallestEven( $n )
{
if ( $n == 1)
return 0;
return pow(10, $n - 1);
}
$n = 4;
echo smallestEven( $n );
?>
|
Javascript
<script>
function smallestEven(n)
{
if (n == 1)
return 0;
return Math.pow(10, n - 1);
}
var n = 4;
document.write(smallestEven(n));
</script>
|
Time Complexity: O(log n).
Share your thoughts in the comments
Please Login to comment...