📅  最后修改于: 2023-12-03 15:37:14.925000             🧑  作者: Mango
This is a programming question from the ISRO CS 2013 exam. The question involves finding the sum of numbers in a given range that are divisible by a specific number.
Given three integers A
, B
, and K
, find the sum of numbers between A
and B
(inclusive) that are divisible by K
.
The input consists of three integers A
, B
, and K
separated by a space.
The output is a single integer - the sum of numbers between A
and B
(inclusive) that are divisible by K
.
Input:
10 20 3
Output:
18
To solve this problem, we need to find the sum of multiples of K in range A and B. We can do this using a loop that starts from A and increments by 1 until it reaches B.
For each number in this range, we can check if it is divisible by K using the modulo operator %
. If the remainder is 0, then the number is divisible by K and we can add it to the running sum.
After iterating through all the numbers in the range, we can return the sum.
Here is the Python code for the solution:
def sum_of_divisible(A, B, K):
# Initialize the sum to 0
sum = 0
# Loop through all the numbers in the range A to B
for i in range(A, B+1):
# Check if i is divisible by K
if i % K == 0:
# Add i to the sum if it is divisible by K
sum += i
# Return the sum
return sum
To use this function, we can call it with the input values and print the result:
A, B, K = [int(x) for x in input().split()]
result = sum_of_divisible(A, B, K)
print(result)
In this article, we have solved the ISRO CS 2013 question 5 using Python. We have used a loop to iterate through the range and check if each number is divisible by K. Finally, we have returned the sum of all the divisible numbers.