Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 23 Feb, 2021

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 
 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



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




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

Java




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

Python3




# 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

C#




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

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to print the largest n-digit even
// and odd numbers in octal number system
function findNumbers(n)
{
 
    // Append '7' (N - 1) times
    var ans = "";
    for (var i = 0; i < n - 1; i++)
        ans += '7';
 
    // Append '6' for an even number
    var even = ans + '6';
 
    // Append '7' for an odd number
    var odd = ans + '7';
 
    document.write("Even : " + even + "<br>");
    document.write("Odd : " + odd + "<br>");
}
 
// Driver code
    var n = 4;
 
    findNumbers(n);
 
// This code is contributed by Mayank Tyagi
 
</script>
Output: 
Even : 7776
Odd : 7777

 

Time Complexity: O(n)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!