Skip to content
Related Articles

Related Articles

Improve Article

Largest Even and Odd N-digit numbers of base B

  • Last Updated : 22 Jul, 2021

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

Examples: 

Input: N = 2, B = 5 
Output: 
Even = 24 
Odd = 23 
Explanation: 
Largest Even Number of 2 digits in base 5 = 44 which is 24 in decimal form. 
Largest Odd Number of 2 digits in base 5 = 43 which is 23 in decimal form.

Input: N = 2, B = 10 
Output: 
Even = 98 
Odd = 99 

Approach: 
To get the largest Even and Odd N-digits number of base B in decimal form is given by: 



  1. If Base B is Even, then: 
    • Largest N-digit even number is (BN – 2).
    • Largest N-digit odd number is (BN – 1).
  2. If Base B is Odd, then: 
    • Largest N-digit Even number is (BN – 1).
    • Largest N-digit Odd number is (BN – 2).

Below is the implementation of the above approach:  

C++




// C++ implementation of the
// above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the largest
// N-digit even and odd numbers
// of base B
void findNumbers(int n, int b)
{
    // Initialise the Number
    int even = 0, odd = 0;
 
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
 
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = pow(b, n) - 2;
 
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = pow(b, n) - 1;
    }
 
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
 
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = pow(b, n) - 1;
 
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = pow(b, n) - 2;
    }
    cout << "Even Number = " << even << '\n';
    cout << "Odd Number = " << odd;
}
 
// Driver's Code
int main()
{
    int N = 2, B = 5;
 
    // Function to find the
    // numbers
    findNumbers(N, B);
    return 0;
}

Java




// Java implementation of the
// above approach
import java.util.*;
 
class GFG{
  
// Function to print the largest
// N-digit even and odd numbers
// of base B
static void findNumbers(int n, int b)
{
    // Initialise the Number
    double even = 0, odd = 0;
  
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
  
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.pow(b, n) - 2;
  
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.pow(b, n) - 1;
    }
  
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
  
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.pow(b, n) - 1;
  
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.pow(b, n) - 2;
    }
    System.out.println("Even Number = " +  (int)even );
    System.out.print("Odd Number = " +  (int)odd);
}
  
// Driver's Code
public static void main(String[] args)
{
    int N = 2, B = 5;
  
    // Function to find the
    // numbers
    findNumbers(N, B);
}
}
 
// This code is contributed by Rajput-Ji

Python3




# Python implementation of the
# above approach
 
# Function to print the largest
# N-digit even and odd numbers
# of base B
def findNumbers(n, b):
    # Initialise the Number
    even = 0;
    odd = 0;
 
    # If Base B is even, then
    # B^n will give largest
    # Even number of N+1 digit
    if (b % 2 == 0):
 
        # To get even number of
        # N digit subtract 2 from
        # B^n
        even = pow(b, n) - 2;
 
        # To get odd number of
        # N digit subtract 1 from
        # B^n
        odd = pow(b, n) - 1;
     
 
    # If Base B is odd, then
    # B^n will give largest
    # Odd number of N+1 digit
    else:
 
        # To get even number of
        # N digit subtract 1 from
        # B^n
        even = pow(b, n) - 1;
 
        # To get odd number of
        # N digit subtract 2 from
        # B^n
        odd = pow(b, n) - 2;
     
    print("Even Number = ",int(even));
    print("Odd Number = ", int(odd));
 
# Driver's Code
if __name__ == '__main__':
    N = 2;
    B = 5;
 
    # Function to find the
    # numbers
    findNumbers(N, B);
     
# This code is contributed by 29AjayKumar

C#




// C# implementation of the
// above approach
using System;
 
class GFG{
   
// Function to print the largest
// N-digit even and odd numbers
// of base B
static void findNumbers(int n, int b)
{
    // Initialise the Number
    double even = 0, odd = 0;
   
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
   
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.Pow(b, n) - 2;
   
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.Pow(b, n) - 1;
    }
   
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
   
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.Pow(b, n) - 1;
   
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.Pow(b, n) - 2;
    }
    Console.WriteLine("Even Number = " +  (int)even );
    Console.Write("Odd Number = " +  (int)odd);
}
   
// Driver's Code
public static void Main(String[] args)
{
    int N = 2, B = 5;
   
    // Function to find the
    // numbers
    findNumbers(N, B);
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
// Javascript implementation of the
// above approach
 
// Function to print the largest
// N-digit even and odd numbers
// of base B
function findNumbers(n, b)
{
 
    // Initialise the Number
    var even = 0, odd = 0;
 
    // If Base B is even, then
    // B^n will give largest
    // Even number of N+1 digit
    if (b % 2 == 0) {
 
        // To get even number of
        // N digit subtract 2 from
        // B^n
        even = Math.pow(b, n) - 2;
 
        // To get odd number of
        // N digit subtract 1 from
        // B^n
        odd = Math.pow(b, n) - 1;
    }
 
    // If Base B is odd, then
    // B^n will give largest
    // Odd number of N+1 digit
    else {
 
        // To get even number of
        // N digit subtract 1 from
        // B^n
        even = Math.pow(b, n) - 1;
 
        // To get odd number of
        // N digit subtract 2 from
        // B^n
        odd = Math.pow(b, n) - 2;
    }
    document.write("Even Number = " + even + "<br>");
    document.write("Odd Number = " + odd);
}
 
// Driver's Code
var N = 2, B = 5;
 
// Function to find the
// numbers
findNumbers(N, B);
 
// This code is contributed by rrrtnx.
</script>
Output: 
Even Number = 24
Odd Number = 23

 

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :