Largest N digit number in Base B
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: 99Input: 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 bvoid 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 Codeint main(){ // Given Number and Base int N = 2, B = 5; // Function Call findNumbers(N, B); return 0;} |
Java
// Java program for the approachimport java.util.*;class GFG{ // Function to print the largest// N-digit numbers of base bstatic 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 Codepublic 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 bdef 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 baseN, B = 2, 5# Function CallfindNumbers(N, B)# This code is contributed by jrishabh99 |
C#
// C# program for the approachusing System;class GFG{ // Function to print the largest// N-digit numbers of base bstatic 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 Codepublic 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 bfunction 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> |
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


