If I understand correctly, the nested If statements and the loop produce the following:
count = 0
nums = 1
1 is >= 1 and <= 1000, so if statement executes
k = 1000
1000 >= 1 and <= 1000, so if statement executes
for loop: range goes from 0 to 1 (only two loops)
loop i = 0: 0 / 1000 equals 0, so increment count
loop i = 1: 1 / 1000 does not equal 0, so don't increment
Final outcome: Count == 1
May need to rethink your loop so that it produces the correct results.
I'd also recommend validating user input when it's provided rather than as part of your divisor count logic. This will separate your divisor count logic and make it easier to understand and process mentally.
Just a few (personal) formatting tips.
When you have multiple variables of the same type, you can declare them on the same line like so: int l = 0, r = 0, k = 0, count = 0;
Likewise with cin cin >> l >> r >> k;
You can also move l++ inside the brackets of the for loop.
1 2 3 4
for (int i = 0; l<=r; i++, l++)
{
// ...
}
Try to give your variables more descriptive names that relate to its purpose. For example,
1 2 3 4 5
// bad names
int l = 0, r = 0, k = 0;
// better names
int lower_bound = 0, upper_bound = 0, divisor = 0;