 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Largest number smaller than or equal to N divisible by K in C++
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.
#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.
Advertisements
 