# Largest number smaller than or equal to N divisible by K in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this tutorial, we are going to write a program that finds the number that is smaller than or equal to N and divisible by k.

Let's see the steps to solve the problem.

• Initialise the numbers n and k.
• Find the remainder with modulo operator.
• If the remainder is zero, then return n.
• Else return n - remainder.

## Example

Let's see the code.

Live Demo

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

## Output

If you run the above code, then you will get the following result.

30

## Conclusion

If you have any queries in the tutorial, mention them in the comment section.