C++ program to find largest or equal number of A whose sum of digits is divisible by 4

C++Server Side ProgrammingProgramming

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
raja
Updated on 03-Mar-2022 05:39:41

Advertisements