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