Thursday, September 4, 2025
HomeData Modelling & AISmallest Even number with N digits

Smallest Even number with N digits

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++




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


Java




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


Python3




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


C#




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


PHP




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


Javascript




<script>
// Javascript implementation of the approach
 
// Function to return smallest even
// number with n digits
function smallestEven(n)
{
    if (n == 1)
        return 0;
    return Math.pow(10, n - 1);
}
 
// Driver Code
var n = 4;
document.write(smallestEven(n));
 
// This code is contributed by rrrtnx.
</script>


Output: 

1000

 

Time Complexity: O(log n).
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11856 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS