You have been given 3 integers - l, r, and k. Find how many numbers between l and r (both inclusive) are divisible by k. You do not need to print these numbers, you just have to find their count.
Example:
Input: l = 1, r = 10, k = 1
Output: 10
Approach
C++
#include <bits/stdc++.h>using namespace std;int countDivisors(int l, int r, int k){int cnt = 0;for (int i = l; i <= r; i++){if (i % k == 0)cnt++;}return cnt;}int main(){int l = 1, r = 10, k = 1;cout << countDivisors(l, r, k) << "\n";return 0;}
No comments:
Post a Comment