Related Articles

Related Articles

Largest Even and Odd N-digit numbers in Hexadecimal Number System
  • Last Updated : 25 Mar, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Even: FE
Odd: FF

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
Recommended Articles
Page :