Largest N digit number in Base B

Given a positive interger 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

24

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.