Largest Even and Odd N-digit numbers in Octal Number System
Last Updated :
16 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void findNumbers( int n)
{
string ans = string(n - 1, '7' );
string even = ans + '6' ;
string odd = ans + '7' ;
cout << "Even : " << even << endl;
cout << "Odd : " << odd << endl;
}
int main()
{
int n = 4;
findNumbers(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void findNumbers( int n)
{
String ans = "" ;
for ( int i = 0 ; i < n - 1 ; i++)
ans += '7' ;
String even = ans + '6' ;
String odd = ans + '7' ;
System.out.println( "Even : " + even);
System.out.println( "Odd : " + odd);
}
public static void main(String args[])
{
int n = 4 ;
findNumbers(n);
}
}
|
Python3
def findNumbers(N) :
ans = '7' * (N - 1 )
even = ans + '6' ;
odd = ans + '7' ;
print ( "Even : " , even);
print ( "Odd : " , odd );
if __name__ = = "__main__" :
n = 4 ;
findNumbers(n);
|
C#
using System;
class GFG
{
static void findNumbers( int n)
{
String ans = "" ;
for ( int i = 0; i < n - 1; i++)
ans += '7' ;
String even = ans + '6' ;
String odd = ans + '7' ;
Console.WriteLine( "Even : " + even);
Console.WriteLine( "Odd : " + odd);
}
public static void Main(String []args)
{
int n = 4;
findNumbers(n);
}
}
|
Javascript
<script>
function findNumbers(n)
{
var ans = "" ;
for ( var i = 0; i < n - 1; i++)
ans += '7' ;
var even = ans + '6' ;
var odd = ans + '7' ;
document.write( "Even : " + even + "<br>" );
document.write( "Odd : " + odd + "<br>" );
}
var n = 4;
findNumbers(n);
</script>
|
Output:
Even : 7776
Odd : 7777
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...