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> |
Even: FE Odd: FF
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!