Largest number smaller than or equal to N divisible by K

Given a number N and a number K, the task is to find the largest number smaller than or equal to N which is divisible by K.

Examples:

Input: N = 45, K = 6
Output: 42
42 is the largest number smaller than 
or equal to 45 which is divisible by 6.

Input: N = 11, K = 3
Output: 9


Approach: The idea is to divide the N by K. If the remainder is 0 then print N else print N – remainder.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the largest number
// smaller than or equal to N
// that is divisible by k
int findNum(int N, int K)
{
    int rem = N % K;
  
    if (rem == 0)
        return N;
    else
        return N - rem;
}
  
// Driver code
int main()
{
    int N = 45, K = 6;
  
    cout << "Largest number smaller than or equal to "<< N
         << "\nthat is divisible by "<< K  << " is " << findNum(N, K);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the 
// above approach
import java.lang.*;
import java.util.*;
  
class GFG
{
// Function to find the largest number
// smaller than or equal to N
// that is divisible by k
static int findNum(int N, int K)
{
    int rem = N % K;
  
    if (rem == 0)
        return N;
    else
        return N - rem;
}
  
// Driver code
public static void main(String args[])
{
    int N = 45, K = 6;
  
    System.out.print("Largest number smaller "
                       "than or equal to " + N + 
                 "\nthat is divisible by " + K + 
                        " is " + findNum(N, K));
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
  
# Function to find the largest number smaller 
# than or equal to N that is divisible by k
def findNum(N, K):
       
    rem = N % K
    if(rem == 0):
        return N
    else:
        return N - rem
  
# Driver code
if __name__=='__main__':
      
    N = 45
    K = 6
    print("Largest number smaller than or equal to" + 
           str(N) + "that is divisible by" + str(K) + 
                                "is", findNum(N, K)) 
  
# This code is contributed by
# Kirti_Mangal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
  
class GFG
{
// Function to find the largest number
// smaller than or equal to N
// that is divisible by k
static int findNum(int N, int K)
{
    int rem = N % K;
  
    if (rem == 0)
        return N;
    else
        return N - rem;
}
  
// Driver code
public static void Main()
{
    int N = 45, K = 6;
  
    Console.Write("Largest number smaller "
                     "than or equal to "+ N + 
               "\nthat is divisible by "+ K + 
                     " is " + findNum(N, K));
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the 
// above approach
  
// Function to find the largest 
// number smaller than or equal 
// to N that is divisible by k
function findNum($N, $K)
{
    $rem = $N % $K;
  
    if ($rem == 0)
        return $N;
    else
        return $N - $rem;
}
  
// Driver code
$N = 45 ;
$K = 6 ;
  
echo"Largest number smaller than or equal to ", $N
    "\nthat is divisible by ", $K, " is "
    findNum($N, $K);
      
// This code is contributed by ANKITRAI1
?>

chevron_right


Output:

Largest number smaller than or equal to 45
that is divisible by 6 is 42


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.