# Largest Even and Odd N-digit numbers of base B

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `using` `namespace` `std; ` ` `  `// Function to print the largest ` `// N-digit even and odd numbers ` `// of base B ` `void` `findNumbers(``int` `n, ``int` `b) ` `{ ` `    ``// Intialise 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) ` `{ ` `    ``// Intialise 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): ` `    ``# Intialise 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) ` `{ ` `    ``// Intialise 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 `

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.

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 :

1

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