Skip to content
Related Articles

Related Articles

Improve Article

Largest Even and Odd N-digit numbers in Hexadecimal Number System

  • Last Updated : 05 Apr, 2021

Given an integer N, the task is to find the largest even and odd N-digit numbers in Hexadecimal Number System.
Examples: 
 

Input: N = 1 
Output: 
Even: E 
Odd: F
Input: N = 2 
Output: 
Even: FE 
Odd: FF 
 

 

Approach: To get the largest number, the digits of the number have to be maximum possible. Since in the hexadecimal number system, the maximum digit is ‘F’. So, generate ‘F’ (N – 1) times and then append ‘E’ for even and ‘F’ for odd in the end.
Below is the implementation of the above approach: 
 

C++




// C++ implementation of the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the largest n-digit even
// and odd numbers in hexadecimal number system
void findNumbers(int n)
{
 
    // Append 'F' (N - 1) times
    string ans = string(n - 1, 'F');
 
    // Append 'E' for an even number
    string even = ans + 'E';
 
    // Append 'F' for an odd number
    string odd = ans + 'F';
 
    cout << "Even: " << even << endl;
    cout << "Odd: " << odd << endl;
}
 
// Driver code
int main()
{
    int n = 2;
 
    findNumbers(n);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
 
    // Function to print the largest n-digit even
    // and odd numbers in hexadecimal number system
    static void findNumbers(int n)
    {
 
        // Append 'F' (N - 1) times
        String ans = string(n - 1, 'F');
 
        // Append 'E' for an even number
        String even = ans + 'E';
 
        // Append 'F' for an odd number
        String odd = ans + 'F';
 
        System.out.print("Even: " + even + "\n");
        System.out.print("Odd: " + odd + "\n");
    }
 
    private static String string(int n, char c)
    {
        String str = "";
        for (int i = 0; i < n; i++)
            str += c;
        return str;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 2;
 
        findNumbers(n);
    }
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 implementation of the approach
 
# Function to print the largest n-digit even
# and odd numbers in hexadecimal number system
def findNumbers(n) :
 
    # Append 'F' (N - 1) times
    ans = 'F'*(n - 1);
 
    # Append 'E' for an even number
    even = ans + 'E';
 
    # Append 'F' for an odd number
    odd = ans + 'F';
 
    print("Even: " , even);
    print( "Odd: " , odd);
 
# Driver code
if __name__ == "__main__" :
 
    n = 2;
 
    findNumbers(n);
 
# This code is contributed by AnkitRai01

C#




// C# implementation of the approach
using System;
 
class GFG
{
  
    // Function to print the largest n-digit even
    // and odd numbers in hexadecimal number system
    static void findNumbers(int n)
    {
  
        // Append 'F' (N - 1) times
        String ans = strings(n - 1, 'F');
  
        // Append 'E' for an even number
        String even = ans + 'E';
  
        // Append 'F' for an odd number
        String odd = ans + 'F';
  
        Console.Write("Even: " + even + "\n");
        Console.Write("Odd: " + odd + "\n");
    }
  
    private static String strings(int n, char c)
    {
        String str = "";
        for (int i = 0; i < n; i++)
            str += c;
        return str;
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        int n = 2;
  
        findNumbers(n);
    }
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to print the largest n-digit even
// and odd numbers in hexadecimal number system
function findNumbers(n)
{
 
    // Append 'F' (N - 1) times
    var ans = "F".repeat(n-1);
 
    // Append 'E' for an even number
    var even = ans + 'E';
 
    // Append 'F' for an odd number
    var odd = ans + 'F';
 
    document.write("Even: " + even + "<br>");
    document.write("Odd: " + odd + "<br>");
}
 
// Driver code
var n = 2;
findNumbers(n);
 
</script>
Output: 
Even: FE
Odd: FF

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :