Largest Even and Odd N-digit numbers in C++


In this tutorial, we are going to write a program that finds the largest even and odd number of n digits number.

Let's see the steps to solve the problem.

  • Initialise the number n.
  • The largest odd number is pow(10, n) - 1.
  • The largest even number is odd - 1.

Example

Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void findEvenAndOddNumbers(int n) {
   int odd = pow(10, n) - 1;
   int even = odd - 1;
   cout << "Even: " << even << endl;
   cout << "Odd: " << odd << endl;
}
int main() {
   int n = 6;
   findEvenAndOddNumbers(n);
   return 0;
}

Output

If you run the above code, then you will get the following result.

Even: 999998
Odd: 999999

Conclusion

If you have any queries in the tutorial, mention them in the comment section.

Updated on: 09-Apr-2021

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements