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

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

Examples:

Input: N = 4
Output:
Even : 7776
Odd : 7777



Input: N = 2
Output:
Even : 76
Odd : 77

Approach: To get the largest number, the digits of the number have to be maximum possible. Since in the octal number system, the maximum digit is ‘7’. So, generate ‘7’ (N – 1) times and then append ‘6’ for even and ‘7’ 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 octal number system
void findNumbers(int n)
{
  
    // Append '7' (N - 1) times
    string ans = string(n - 1, '7');
  
    // Append '6' for an even number
    string even = ans + '6';
  
    // Append '7' for an odd number
    string odd = ans + '7';
  
    cout << "Even : " << even << endl;
    cout << "Odd : " << odd << endl;
}
  
// Driver code
int main()
{
    int n = 4;
  
    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 octal number system
static void findNumbers(int n)
{
  
    // Append '7' (N - 1) times
    String ans = "";
    for (int i = 0; i < n - 1; i++)
        ans += '7';
  
    // Append '6' for an even number
    String even = ans + '6';
  
    // Append '7' for an odd number
    String odd = ans + '7';
  
    System.out.println("Even : " + even);
    System.out.println("Odd : " + odd);
}
  
// Driver code
public static void main(String args[]) 
{
    int n = 4;
  
    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 octal number system 
def findNumbers(N) :
  
    # Append '7' (N - 1) times 
    ans = '7' * (N - 1)
  
    # Append '6' for an even number 
    even = ans + '6'
  
    # Append '7' for an odd number 
    odd = ans + '7'
  
    print("Even : ", even); 
    print("Odd : ", odd ); 
  
# Driver code 
if __name__ == "__main__"
  
    n = 4
  
    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 octal number system
static void findNumbers(int n)
{
  
    // Append '7' (N - 1) times
    String ans = "";
    for (int i = 0; i < n - 1; i++)
        ans += '7';
  
    // Append '6' for an even number
    String even = ans + '6';
  
    // Append '7' for an odd number
    String odd = ans + '7';
  
    Console.WriteLine("Even : " + even);
    Console.WriteLine("Odd : " + odd);
}
  
// Driver code
public static void Main(String []args) 
{
    int n = 4;
  
    findNumbers(n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

Even : 7776
Odd : 7777

Time Complexity: O(n)



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.



Improved By : AnkitRai01, 29AjayKumar