divisors count

hi there!
i don't know where i did wrong in my code
i wanna count the numbers between
l and r which (both inclusive) are divisible by
k

for this input(1000 1000 999) it should answer 0 but my output is 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace std;
int main()
{
    int l=0;
    int r=0;
    int k=0;
    int count=0;
    int nums=0;
    cin>>l;
    cin>>r;
    cin>>k;
    nums=r-l+1;
    if(nums>=1 && nums<=1000)
    {
        if(k>=1 && k<=1000)
        {
              for(int i=0;i<=nums;++i)
        {
            if(i%k==0)
            {
                count++;
            }
        }
     cout<<count<<endl;
        }
    
      
    }
    
    return 0;
}
Adding cout << "checking " << i << " % " << k << '\n'; before line 20 Reveals 2 problems:
checking 0 % 999
checking 1 % 999

So you're checking the wrong numbers and you're checking 2 numbers instead of 1.

Why not just use l and r in the for loop directly?
1
2
3
4
5
    for (int i = l; i <= r; ++i) {
	if (i % k == 0) {
	    count++;
	}
    }

Actually, don't use l as a variable name. It's too easy to mistake it for 1. Use left and right instead.
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.
1
2
3
4
5
 for (int i = l; i <= r; ++i) {
	if (i % k == 0) {
	    count++;
	}
    }


i used this loop instead but still doesn't work for this input(1000 1000 999)


i can't understand it's logic
:((((((((((((
Your for loop seems to work for the numbers 1000 1000 999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
  int low = 0;
  int high = 0;
  int divisor = 0;
  int count = 0;
  cout << "Low number: ";
  cin >> low;
  cout << "High number: ";
  cin >> high;
  cout << "Divisor: ";
  cin >> divisor;
  for (int num = low; num <= high; ++num) 
  {
    if (num % divisor == 0) 
    {
      count++;
    }
  }
  cout << "Count = " << count;
  return 0;
}

OUTPUT

Low number: 1000
High number: 1000
Divisor: 999
Count = 0

Here's a little bit simpler way of doing it. This will not test if your high or low are divisible but can be easily modified to do so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main() 
{

	int low, high, Divisor, total =0;
	cout << "Low: ";
	cin >> low;
	cout << "How: ";
	cin >> high;
	cout << "Divisor: ";
	cin >> Divisor;
	
	low++;

	for (int count = 0; low < high; count++)
	{
		if ((low%Divisor) == 0)
		{
			total++;
		}
		low++;
	}

	cout << total << endl;

	return 0;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;
 
int main()
{
    int l=0;
    int r=0;
    int k=0;
    int count=0;
    //int nums=0;
    cin>>l;
    cin>>r;
    cin>>k;
    for (int i = 0; l<=r; i++)
	{
		if ((l%k) == 0)
		{
			count++;
		}
		l++;
	}

	cout << count << endl;
  
    
    return 0;
}


thank you soooo much
problem solved
yeah it was the simplest way
how i thought about it haard:)))))))))))))))
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;
i will keep them in mind
thanks for personal tips ( integralfx )
i used this loop instead but still doesn't work for this input(1000 1000 999)

It works for me. Are you sure you typed it in right? Here it is with longer variable names:
1
2
3
4
5
    for (int num = left; num <= right; ++num) {
        if (num % k == 0) {
            count++;
        }
    }

i can't understand it's logic

This simply iterates num from the left number to the right one. If num is divisble by k then it increments count.

joe864864, in this loop, what is the purpose of count?
1
2
3
4
5
6
7
8
	for (int count = 0; low < high; count++)
	{
		if ((low%Divisor) == 0)
		{
			total++;
		}
		low++;
	}


Melinda345, in this loop, what is the purpose of i?
1
2
3
4
5
6
7
8
    for (int i = 0; l<=r; i++)
	{
		if ((l%k) == 0)
		{
			count++;
		}
		l++;
	}

(dhayden)Are you sure you typed it in right?

yes i'm sure
1
2
3
4
5
6
7
8
 for (int i = 0; l<=r; i++)
	{
		if ((l%k) == 0)
		{
			count++;
		}
		l++;
	


it will check that are we at the end or not
:)

Last edited on
Topic archived. No new replies allowed.