
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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