Count Divisors

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 lint rint k)
{

    int cnt = 0;
    for (int i = li <= ri++)
    {
        if (i % k == 0)
            cnt++;
    }
    return cnt;
}
int main()
{
    int l = 1r = 10k = 1;

    cout << countDivisors(lrk<< "\n";

    return 0;
}


No comments:

Post a Comment