- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to find largest or equal number of A whose sum of digits is divisible by 4
Suppose we have a number A. We have to find nearest larger or equal interesting number for A. A number is said to be interesting number if its sum of digits is divisible by 4.
So, if the input is like A = 432, then the output will be 435, because 4 + 3 + 5 = 12 which is divisible by 4.
Steps
To solve this, we will follow these steps −
while (A / 1000 + A mod 1000 / 100 + A mod 100 / 10 + A mod 10) mod 4 is not equal to 0, do: (increase A by 1) return A
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int A) { while ((A / 1000 + A % 1000 / 100 + A % 100 / 10 + A % 10) % 4 != 0) { A++; } return A; } int main() { int A = 432; cout << solve(A) << endl; }
Input
432
Output
435
Advertisements