给定整数N ,任务是找到最大的偶数和奇数N位数字。
例子:
Input: N = 4
Output:
Even = 9998
Odd = 9999
Input: N = 2
Output:
Even = 98
Odd = 99
方法:
- 最大的N位偶数将是(10 n )– 2,因为不同N值的序列将是8、98、998、9988等。
- 同样,对于系列9、99、999、9999,…. ,最大N位奇数将为(10 n )– 1 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the largest n-digit
// even and odd numbers
void findNumbers(int n)
{
int odd = pow(10, n) - 1;
int even = odd - 1;
cout << "Even = " << even << endl;
cout << "Odd = " << odd;
}
// 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
static void findNumbers(int n)
{
int odd = (int)Math.pow(10, n) - 1;
int even = odd - 1;
System.out.println("Even = " + even);
System.out.print("Odd = " + odd);
}
// Driver code
public static void main(String args[])
{
int n = 4;
findNumbers(n);
}
}
C#
// C# implementation of the approach
using System;
class GFG {
// Function to print the largest n-digit
// even and odd numbers
static void findNumbers(int n)
{
int odd = (int)Math.Pow(10, n) - 1;
int even = odd - 1;
Console.WriteLine("Even = " + even);
Console.Write("Odd = " + odd);
}
// Driver code
public static void Main()
{
int n = 4;
findNumbers(n);
}
}
Python3
# Python3 implementation of the approach
# Function to print the largest n-digit
# even and odd numbers
def findNumbers(n):
odd = pow(10, n) - 1
even = odd - 1
print("Even = ", even)
print("Odd = ", odd)
# Driver code
n = 4
findNumbers(n)
# This code is contributed by ihritik
PHP
Javascript
输出:
Even = 9998
Odd = 9999