find all numbers divisible by 5 and 13

write a program to find all the numbers between 1 and N that are divisible by 5 and 13 ( the program should accept ' N ' from the user )
Sounds like a straight forward task.

1. Ask the user to input N

2. Use a for loop from 1 to N

3. Check if the number divides by 5 without remainder, do the same for 13
I already did that
int n;
int I;
cout <<"enter the value of n : ";
cin>>n;
cout <<"all the numbers between 1 and N that are divisible by 5 and 13 are:";
for (i=1 ; i <=n ; i++)
{
if (n%5==0 && n%13==0);
{
cout>>i;
}
}
return 0;
}
but it doesn't work
1
2
3
4
if (n%5==0 && n%13==0);
{
cout>>i;
}

There are multiple problems with your code, but they are just minor things. This is where the main issue is.
The problem is is that you're checking the remainder of n. If the user enters 10, n will be set as 10, but 10 isn't evenly divisible by 13, so nothing will print.
Now, let's say the user enters 65. n is set to 65 and 65 evenly divides 5 and 13, so it will print out i. But what is i in this case? It's the numbers between 1 and n, inclusive. Thus, it will print out the numbers between 1 and n.
See if you can figure out what's wrong. It's something so simple you'll want to kick yourself. Good luck! :)
Last edited on
closed account (48T7M4Gy)
Another observation. If a number x is divisible by both 5 and 13 then it is divisible by 65, so the check should simply be, if x % 65 == 0

if x is divisible by 5 or 13 then the multiple test is required.
You have a semicolon after your "if" conditional. Take it out. Otherwise the consequent block will always execute.
Topic archived. No new replies allowed.