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++
// 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 |
Even : 7776 Odd : 7777
Time Complexity: O(n)
Recommended Posts:
- Largest number in [2, 3, .. n] which is co-prime with numbers in [2, 3, .. m]
- Program to Convert Octal Number to Binary Number
- Largest N digit number divisible by given three numbers
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Check if number is palindrome or not in Octal
- Check if Decimal representation of an Octal number is divisible by 7
- Largest Even and Odd N-digit numbers
- Smallest and Largest sum of two n-digit numbers
- Largest palindrome which is product of two n-digit numbers
- Sum and product of k smallest and k largest composite numbers in the array
- Largest of two distinct numbers without using any conditional statements or operators
- Sum and product of k smallest and k largest prime numbers in the array
- Number of times the largest perfect square number can be subtracted from N
- Find the largest good number in the divisors of given number N
- Largest even number that can be formed by any number of swaps
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.



