C++ code to find greater number whose factor is k


Suppose we have two numbers n and k. We have to find smallest integer x which is greater than n and divisible by k.

So, if the input is like n = 5; k = 3, then the output will be 6.

Steps

To solve this, we will follow these steps −

return n + k - (n mod k)

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int n, int k){
   return n + k - n % k;
}
int main(){
   int n = 5;
   int k = 3;
   cout << solve(n, k) << endl;
}

Input

5, 3

Output

6

Updated on: 29-Mar-2022

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements