
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
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
Advertisements