

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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.
- Related Questions & Answers
- Largest number less than N with digit sum greater than the digit sum of N in C++
- Largest Even and Odd N-digit numbers in C++
- Kth prime number greater than N in C++
- Largest N digit number divisible by given three numbers in C++
- JavaScript - Find the smallest n digit number or greater
- Largest subarray having sum greater than k in C++
- Queries for greater than and not less than using C++
- Python – Average of digit greater than K
- Count n digit numbers not having a particular digit in C++
- Program to find number not greater than n where all digits are non-decreasing in python
- Product of N with its largest odd digit in C
- Find largest number smaller than N with same set of digits in C++
- Largest number smaller than or equal to N divisible by K in C++
- Number of nodes greater than a given value in n-ary tree in C++
- Count numbers with difference between number and its digit sum greater than specific value in C++
Advertisements