
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- Largest K digit number divisible by X in C++
- C++ Program for Largest K digit number divisible by X?
- Java Program for Largest K digit number divisible by X
- Find largest number smaller than N with same set of digits in C++
- C++ Program for the Largest K digit number divisible by X?
- Euler’s Totient function for all numbers smaller than or equal to n in java
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Largest N digit number divisible by given three numbers in C++
- C++ program to find largest or equal number of A whose sum of digits is divisible by 4
- Minimum numbers which is smaller than or equal to N and with sum S in C++
- How to get the largest integer less than or equal to a number in JavaScript?
- Find nth number that contains the digit k or divisible by k in C++
- Largest set with bitwise OR equal to n in C++
- Find Largest Special Prime which is less than or equal to a given number in C++
- Return the largest integer smaller or equal to the division of the inputs in Numpy

Advertisements