Single File Programming QuestionProblem StatementGiven two integers N and M. The problem is to find the number closest to N and divisible by M. If there is more than one such number, then output the one having the maximum absolute value.Example 1Input: N = 13 , M = 4Output: 12Explanation: 12 is the closest number to 13 which is divisible by 4.Example 2Input: N = -15 , M = 6Output: -18Explanation: -12 and -18 are both similarly close to -15 and divisible by 6. But -18 has the maximum absolute value. So, output is -18Your Task: Write a function named closestNumber which takes the integers N and M as input and returns the answer.Note: This question was asked in the Microsoft coding test.Input format :The input consists of two space-separated integers N and M.Output format :The output prints an integer, representing the number closest to N and divisible by M.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:-1000 ≤ N ≤ 10001 ≤ M ≤ 50Sample test cases :Input 1 :13 4Output 1 :12Input 2 :5 3Output 2 :6Input 3 :-15 6Output 3 :-18Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Question
Single File Programming QuestionProblem StatementGiven two integers N and M. The problem is to find the number closest to N and divisible by M. If there is more than one such number, then output the one having the maximum absolute value.Example 1Input: N = 13 , M = 4Output: 12Explanation: 12 is the closest number to 13 which is divisible by 4.Example 2Input: N = -15 , M = 6Output: -18Explanation: -12 and -18 are both similarly close to -15 and divisible by 6. But -18 has the maximum absolute value. So, output is -18Your Task: Write a function named closestNumber which takes the integers N and M as input and returns the answer.Note: This question was asked in the Microsoft coding test.Input format :The input consists of two space-separated integers N and M.Output format :The output prints an integer, representing the number closest to N and divisible by M.Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:-1000 ≤ N ≤ 10001 ≤ M ≤ 50Sample test cases :Input 1 :13 4Output 1 :12Input 2 :5 3Output 2 :6Input 3 :-15 6Output 3 :-18Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Solution
Here is a Python solution for the problem:
def closestNumber(N, M):
q = 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
# Test the function
print(closestNumber(13, 4)) # Output: 12
print(closestNumber(5, 3)) # Output: 6
print(closestNumber(-15, 6)) # Output: -18
This function works by first finding the quotient q when N is divided by M. It then calculates two possible closest numbers n1 and n2 by multiplying M with q and q+1 (or q-1 if N and M are of different signs). It finally returns the number among n1 and n2 which is closer to N. If both are equally close, it returns the one with the maximum absolute value.
Similar Questions
Single File Programming QuestionProblem Statement In a coding challenge, you have been tasked with a unique problem: to find the maximum of two given integers using only relational operators.Write a program that takes two integer inputs, and uses only relational operators to determine the maximum of the two numbers. Your program should then print the calculated maximum.Input format :The input consists of two integers n and m, separated by space.Output format :The output prints the maximum of the given numbers.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases fall under the following constraints:10 ≤ n, m ≤ 105Sample test cases :Input 1 :78 69Output 1 :78Input 2 :410 725Output 2 :725
Single File Programming QuestionProblem Statement Emma needs your help in deciding which of the two numbers is smaller. Create a program that takes two integers as input, identifies the minimum using a relational operator, and displays it.Input format :The input consists of two space-separated integers.Output format :The output prints the smallest of the given input numbers.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:-107 ≤ input integers ≤ 107Sample test cases :Input 1 :89 98Output 1 :89Input 2 :745 -968Output 2 :-968Input 3 :-2536 -2578Output 3 :-2578Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Single File Programming QuestionProblem StatementMukilan is on a mathematical adventure and wants to explore the absolute values of different numbers. He's seeking a tool to quickly determine the absolute value of any given integer using the abs() function from the math library.Can you help him by creating a program?Input format :The input is an integer n, representing the number to be calculated.Output format :The output displays the absolute value of the input integer n.Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:-1000 ≤ n ≤ 1000Sample test cases :Input 1 :-1000Output 1 :The absolute value of -1000 is 1000Input 2 :586Output 2 :The absolute value of 586 is 586Input 3 :1000Output 3 :The absolute value of 1000 is 1000Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0
Single File Programming QuestionProblem StatementSam is on a quest to decipher enchanted scrolls in an ancient temple that requires unveiling the HCF (Greatest Common Divisor) and LCM (Least Common Multiple) of two numbers. Write a program where Sam enters two positive integers, calculates the HCF and LCM using a for loop, and displays them.ExampleInput:6 3Output:HCF = 3LCM = 6Explanation:HCF (Greatest Common Divisor) of two numbers is the largest number that divides both of them without leaving a remainder.For 6 and 3, the common divisors are 1 and 3. The largest of these is 3, so the HCF is 3.LCM (Least Common Multiple) of two numbers is the smallest number that is a multiple of both.To find the LCM, we typically use the formula: LCM(a, b) = (a * b) / HCF(a, b).For 6 and 3, (6 * 3) / 3 = 18 / 3 = 6. So, the LCM is 6.Note: This question helps in clearing AMCAT exam.Input format :The input consists of two space-separated integers: n1 and n2.Output format :The first line prints "HCF = " followed by an integer which is the HCF of n1 and n2.The second line prints "LCM = " followed by an integer which is the LCM of n1 and n2.Refer to the sample outputs for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n1, n2 ≤ 1000Sample test cases :Input 1 :6 3Output 1 :HCF = 3LCM = 6Input 2 :1000 1Output 2 :HCF = 1LCM = 1000
Single File Programming QuestionProblem StatementYou are given a positive integer X. Write a simple program to find the second largest digit in the given number X.Example 1:Input: X = 9765Output: 7Explanation: The second largest digit in 9765 is 7.Example 2:Input: X = 11Output: -1Explanation: If there is no second-largest digit found in the given number, print -1.Note: This question was asked in the Wipro coding test.Input format :The input consists of a positive integer X.Output format :The output prints an integer, representing the second largest digit in the given number X.If there is no second largest digit, the output prints "-1".Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:500 ≤ X ≤ 106Sample test cases :Input 1 :99856Output 1 :8Input 2 :555Output 2 :-1Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.