Dado un número entero N , la tarea es encontrar los números de N dígitos pares e impares más grandes.
Ejemplos:
Entrada: N = 4
Salida:
Par = 9998
Impar = 9999
Entrada: N = 2
Salida:
Par = 98
Impar = 99
Acercarse:
- El número par de N dígitos más grande será ( 10 n ) – 2 porque la serie para diferentes valores de N será 8, 98, 998, 9998, …..
- De manera similar, el número impar de N dígitos más grande será (10 n ) – 1 para la serie 9, 99, 999, 9999, …..
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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
<?php // PHP implementation of the approach // Function to print the largest n-digit // even and odd numbers function 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 ?>
Javascript
<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>
Producción:
Even = 9998 Odd = 9999
Complejidad de tiempo: O (log n)
Espacio Auxiliar: O(1)