Thursday, January 16, 2025
Google search engine
HomeData Modelling & AIFind the largest N digit multiple of N

Find the largest N digit multiple of N

Given a number N, the task is to find the largest N-digit multiple of N
Examples: 
 

Input: N = 2 
Output: 98 
Explanation: 
98 is the largest multiple of 2 and is of 2 digits.
Input: N = 3 
Output: 999 
Explanation: 
999 is the largest multiple of 3 and is of 3 digits. 
 

 

Approach: The idea is to make an observation. 
 

  • If we observe carefully, a series will be formed as 9, 98, 999, 9996, 99995, …
  • In the above series, the N-th term can be calculated as: 
     

N*\lfloor \frac{10^N-1}{N} \rfloor
 

  • Therefore, the number N is taken as the input and the above formula is implemented.

Below is the implementation of the above approach:
 

C++




// C++ program to find largest multiple
// of N containing N digits
#include <iostream>
#include <math.h>
using namespace std;
 
// Function to find the largest
// N digit multiple of N
 
void smallestNumber(int N)
{
    cout << N * floor((pow(10, N) - 1) / N);
}
 
// Driver code
int main()
{
    int N = 2;
    smallestNumber(N);
 
    return 0;
}


Java




// Java program to find largest multiple
// of N containing N digits
import java.util.*;
class GFG{
 
// Function to find the largest
// N digit multiple of N
static void smallestNumber(int N)
{
    System.out.print(N * Math.floor((
                         Math.pow(10, N) - 1) / N));
}
 
// Driver code
public static void main(String args[])
{
    int N = 2;
    smallestNumber(N);
}
}
 
// This code is contributed by Nidhi_biet


Python3




# Python3 program to find largest multiple
# of N containing N digits
from math import floor
 
# Function to find the largest
# N digit multiple of N
def smallestNumber(N):
    print(N * floor((pow(10, N) - 1) / N))
 
# Driver code
if __name__ == '__main__':
    N = 2
    smallestNumber(N)
 
# This code is contributed by Mohit Kumar


C#




// C# program to find largest multiple
// of N containing N digits
using System;
class GFG{
 
// Function to find the largest
// N digit multiple of N
static void smallestNumber(int N)
{
    Console.Write(N * Math.Floor((
                      Math.Pow(10, N) - 1) / N));
}
 
// Driver code
public static void Main()
{
    int N = 2;
    smallestNumber(N);
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
 
// javascript program to find largest multiple
// of N containing N digits
 
 
// Function to find the largest
// N digit multiple of N
 
function smallestNumber( N)
{
    document.write( N * Math.floor((Math.pow(10, N) - 1) / N));
}
 
// Driver code
let N = 2;
    smallestNumber(N);
 
// This code is contributed by todaysgaurav
 
</script>


Output: 

98

 

Time Complexity: O(1)

Auxiliary Space: O(1)

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

Recent Comments