 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Largest even digit number not greater than N in C++
In this tutorial, we are going to write a program that finds the largest number whose digits are all even and not greater than the given n.
Let's see the steps to solve the problem.
- Initialise the number n.
- Write a loop from i = n .- Check whether the digits of current number are all even or not.
- If the above condition satisfies, then print the number.
- Else decrement the i.
 
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; int allDigitsEven(int n) {    while (n) {       if ((n % 10) % 2){          return 0;       }       n /= 10;    }    return 1; } int findLargestEvenNumber(int n) {    int i = n;    while (true) {       if (allDigitsEven(i)) {          return i;       }       i--;    } } int main() {    int N = 43;    cout << findLargestEvenNumber(N) << endl;    return 0; }  Output
If you run the above code, then you will get the following result.
42
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
Advertisements
 