Largest Even and Odd N-digit numbers - GeeksforGeeks

25 Mar, 2021
Given an integer N, the task is to find the largest even and odd N-digit numbers.Examples:  
Input: N = 4 Output: Even = 9998 Odd = 9999Input: N = 2 Output: Even = 98 Odd = 99  
 
Approach:  
Largest N-digit even number will be (10n) – 2 because the series for different values of N will be 8, 98, 998, 9998, .....
Similarly, largest N-digit odd number will be (10n) – 1 for the series 9, 99, 999, 9999, .....
Below is the implementation of the above approach: 
C++
Java
C#
Python3
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to print the largest n-digit// even and odd numbersvoid findNumbers(int n){    int odd = pow(10, n) - 1;    int even = odd - 1;    cout << "Even = " << even << endl;    cout << "Odd = " << odd;} // Driver codeint main(){    int n = 4;    findNumbers(n);     return 0;}
// Java implementation of the approachclass 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# implementation of the approachusing 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 implementation of the approach # Function to print the largest n-digit# even and odd numbersdef findNumbers(n):     odd = pow(10, n) - 1    even = odd - 1    print("Even = ", even)    print("Odd = ", odd) # Driver coden = 4findNumbers(n) # This code is contributed by ihritik
<?php// PHP implementation of the approach // Function to print the largest n-digit// even and odd numbersfunction findNumbers($n){    $odd = pow(10, $n) - 1;    $even = $odd - 1;    echo "Even = $even \n";    echo "Odd = $odd";} // Driver code$n = 4 ;findNumbers($n); // This code is contributed by ihritik?>
<script>    // Javascript implementation of the approach       // Function to print the largest n-digit   // even and odd numbers   function findNumbers(n)   {       var odd = Math.pow(10, n) - 1;       var even = odd - 1;       document.write("Even = " + even+"<br>");       document.write("Odd = " + odd);   }       // Driver code   var n = 4;   findNumbers(n);       // This code is contributed by rrrtnx.     </script>
Even = 9998
Odd = 9999
 
ihritik
rrrtnx
number-theory
school-programming
Mathematical
number-theory
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python
Count ways to reach the n'th stair