human
Can you provide a code to find the number closest to n and divisible by m and the other steps required given the following:
Find the number closest to n and divisible by m | Function to find the number closest to n and divisible by m.
Find the quotient.
1 st possible closest number.
2 nd possible closest number.
if true , then n1 is the required closest number.
else n2 is the required closest number.
Driver program to test above"
Solution in Python:
assistant
#Here is code to find the number closest to n and divisible by m and the other steps required.
 def closestNumber ( n , m ) :
	q = int ( n / m )
	n1 = m * q
	if ( ( n * m ) > 0 ) :
		n2 = ( m * ( q + 1 ) )
	else :
		n2 = ( m * ( q - 1 ) )
	if ( abs ( n - n1 ) < abs ( n - n2 ) ) :
		return n1
	return n2
n = 13 ; m = 4
print ( closestNumber ( n , m ) )
n = - 15 ; m = 6
print ( closestNumber ( n , m ) )
n = 0 ; m = 8
print ( closestNumber ( n , m ) )
n = 18 ; m = - 7
print ( closestNumber ( n , m ) )