Smallest number greater than or equal to N divisible by K
Last Updated :
04 Dec, 2023
Given a number N and a number K, the task is to find the smallest number greater than or equal to N which is divisible by K.
Examples:
Input: N = 45, K = 6
Output: 48
48 is the smallest number greater than or equal to 45
which is divisible by 6.
Input: N = 11, K = 3
Output: 12
Approach:
Approach to solve this problem would be to start from N and check each number one by one until we find a number that is divisible by K. We can use a while loop to keep incrementing N until we find a number that is divisible by K.
Here are the steps of approach:
- We start with N and check each number one by one until we find a number that is divisible by K.
- We use a while loop to keep incrementing N until we find a number that is divisible by K.
- Once we find the smallest number greater than or equal to N that is divisible by K, we return it.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findNum( int N, int K)
{
while (N % K != 0) {
N++;
}
return N;
}
int main()
{
int N = 45, K = 6;
cout << "Smallest number greater than or equal to " << N
<< "\nthat is divisible by " << K << " is " << findNum(N, K);
return 0;
}
|
Java
public class Main {
public static int findNum( int N, int K) {
while (N % K != 0 ) {
N++;
}
return N;
}
public static void main(String[] args) {
int N = 45 , K = 6 ;
int result = findNum(N, K);
System.out.println( "Smallest number greater than or equal to " + N +
"\nthat is divisible by " + K + " is " + result);
}
}
|
Python3
def find_num(N, K):
while N % K ! = 0 :
N + = 1
return N
if __name__ = = "__main__" :
N = 45
K = 6
print (f "Smallest number greater than or equal to {N} \nthat is divisible by {K} is {find_num(N, K)}" )
|
C#
using System;
class Program
{
static int FindNum( int N, int K)
{
while (N % K != 0)
{
N++;
}
return N;
}
static void Main( string [] args)
{
int N = 45, K = 6;
Console.WriteLine($ "Smallest number greater than or equal to {N} " +
$ "that is divisible by {K} is {FindNum(N, K)}" );
}
}
|
Javascript
function findNum(N, K) {
while (N % K !== 0) {
N++;
}
return N;
}
let N = 45;
let K = 6;
console.log(`Smallest number greater than or equal to ${N}\nthat is divisible by ${K} is ${findNum(N, K)}`);
|
Output
Smallest number greater than or equal to 45
that is divisible by 6 is 48
Time Complexity: O(K), where K is the divisor. The while loop will run at most K times before finding a number that is divisible by K.
Space Complexity: O(1), because we are only using a constant amount of extra space to store the input variables
Approach: The idea is to divide the N+K by K. If the remainder is 0 then print N else print N + K – remainder.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int findNum( int N, int K)
{
int rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
int main()
{
int N = 45, K = 6;
cout << "Smallest number greater than or equal to " << N
<< "\nthat is divisible by " << K << " is " << findNum(N, K);
return 0;
}
|
Java
public class GFG{
static int findNum( int N, int K)
{
int rem = (N + K) % K;
if (rem == 0 )
return N;
else
return N + K - rem;
}
public static void main(String []args){
int N = 45 , K = 6 ;
System.out.println( "Smallest number greater than or equal to " + N
+ "\nthat is divisible by " + K + " is " + findNum(N, K));
}
}
|
Python
def findNum(N, K):
rem = (N + K) % K;
if (rem = = 0 ):
return N
else :
return (N + K - rem)
N = 45
K = 6
print ( 'Smallest number greater than' ,
'or equal to' , N,
'that is divisible by' , K,
'is' , findNum( 45 , 6 ))
|
C#
public class GFG{
static int findNum( int N, int K)
{
int rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
static void Main(){
int N = 45, K = 6;
System.Console.WriteLine( "Smallest number greater than or equal to " + N
+ "\nthat is divisible by " + K + " is " + findNum(N, K));
}
}
|
Javascript
<script>
function findNum(N , K)
{
var rem = (N + K) % K;
if (rem == 0)
return N;
else
return N + K - rem;
}
var N = 45, K = 6;
document.write( "Smallest number greater than or equal to " + N
+ "<br>that is divisible by " + K + " is " + findNum(N, K));
</script>
|
PHP
<?php
function findNum( $N , $K )
{
$rem = ( $N + $K ) % $K ;
if ( $rem == 0)
return $N ;
else
return $N + $K - $rem ;
}
$N = 45; $K = 6;
echo "Smallest number greater than " .
"or equal to " , $N ;
echo "\nthat is divisible by " , $K ,
" is " , findNum( $N , $K );
?>
|
Output
Smallest number greater than or equal to 45
that is divisible by 6 is 48
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...