Skip to content
Related Articles

Related Articles

Largest N digit number in Base B

View Discussion
Improve Article
Save Article
  • Last Updated : 22 Mar, 2021
View Discussion
Improve Article
Save Article

Given a positive integer N and base B, the task is to find the largest N-digit numbers of Base B in decimal form.

Examples: 

Input: N = 2, B = 10 
Output: 99

Input: N = 2, B = 5 
Output: 24 

Approach: 
Since there are B digits in base B, so with these digits we can create BN strings of length N. They represent the integers in range 0 to BN – 1 
Therefore, the largest N-digits number of base B in decimal form is given by BN – 1.

Below is the implementation of the above approach: 

C++




// C++ program for the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the largest
// N-digit numbers of base b
void findNumbers(int n, int b)
{
 
    // Find the largest N digit
    // number in base b using the
    // formula B^N - 1
    int largest = pow(b, n) - 1;
 
    // Print the largest number
    cout << largest << endl;
}
 
// Driver Code
int main()
{
    // Given Number and Base
    int N = 2, B = 5;
 
    // Function Call
    findNumbers(N, B);
 
    return 0;
}

Java




// Java program for the approach
import java.util.*;
class GFG{
     
// Function to print the largest
// N-digit numbers of base b
static void findNumbers(int n, int b)
{
 
    // Find the largest N digit
    // number in base b using the
    // formula B^N - 1
    double largest = Math.pow(b, n) - 1;
 
    // Print the largest number
    System.out.println(largest);
}
 
// Driver Code
public static void main(String []args)
{
    // Given Number and Base
    int N = 2, B = 5;
 
    // Function Call
    findNumbers(N, B);
}
}
 
// This code is contributed by Ritik Bansal

Python3




# Python3 program for the above approach
 
# Function to print the largest
# N-digit numbers of base b
def findNumbers(n, b):
     
    # Find the largest N digit
    # number in base b using the
    # formula B^N - 1
    largest = pow(b, n) - 1
 
    # Print the largest number
    print(largest)
 
# Driver Code
 
# Given number and base
N, B = 2, 5
 
# Function Call
findNumbers(N, B)
 
# This code is contributed by jrishabh99

C#




// C# program for the approach
using System;
class GFG{
     
// Function to print the largest
// N-digit numbers of base b
static void findNumbers(int n, int b)
{
 
    // Find the largest N digit
    // number in base b using the
    // formula B^N - 1
    double largest = Math.Pow(b, n) - 1;
 
    // Print the largest number
    Console.Write(largest);
}
 
// Driver Code
public static void Main(String []args)
{
    // Given Number and Base
    int N = 2, B = 5;
 
    // Function Call
    findNumbers(N, B);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript




<script>
// javascript program for the approach
 
// Function to print the largest
// N-digit numbers of base b
function findNumbers( n,  b)
{
 
    // Find the largest N digit
    // number in base b using the
    // formula B^N - 1
    let largest = Math.pow(b, n) - 1;
 
    // Print the largest number
     document.write(largest);
}
 
// Driver Code
 
    // Given Number and Base
    let N = 2, B = 5;
 
    // Function Call
    findNumbers(N, B);
     
    // This code is contributed by aashish1995
 
</script>

Output:

24

Time Complexity: O(1) 
Auxiliary Space: O(1)
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!