Question-
Polycarp knows that if the sum of the digits of a number is divisible by , then the number itself is divisible by . He assumes that the numbers, the sum of the digits of which is divisible by , are also somewhat interesting. Thus, he considers a positive integer interesting if its sum of digits is divisible by .Help Polycarp find the nearest larger or equal interesting number for the given number a.That is, find the interesting number n such that n≥a and n is minimal.
Explanation-1)We have to find a number whose sum is divisible by 4 if the entered number's sum is not divisible by 4 then we have to print the next number whose sum is divisible by 4.
2)Create a function which will extract the digits of the number entered and return its sum to the main function (here the function is named as digit).
3)In main function create an infinite loop which will check if the returned sum from the "digit" function is divisible by 4 or not.
4)If it is divisible then it print that number else it increment the value of entered number till next divisible number is found.
Program-
#include<iostream> using namespace std; int digit(int); int main() { int a; cin>>a; while(1) { int n; n=digit(a); if(n%4==0) break; else a++; } cout<<a; } int digit(int x) //Function to extract the digits and calculate its sum. { int sum=0; while(x!=0) { int rem; rem=x%10; sum=sum+rem; x=x/10; } return sum; }Link to problem on Codeforces-https://codeforces.com/problemset/problem/1183/A