writing a program

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 )
for(i = 65; i < N, i++)
{
if(i%65 == 0)
cout << "found" << i << endl;
}

this is not the same as divisible by 5 OR 13, of course. But the logic and general approach would be similar. I expect that is what you really wanted, so use this as an example, give it a try, and then ask again if you get stuck.
Last edited on
for (i=1; i <=n; i++);
I'm stuck it displays the numbers starting from 1 to the values of n , how can I correct it?
first, for loops do not have a ; on the end. That will compile and work, but it just loops and does nothing however many times, then does the apparent (but is not) loop body once.

Second, its hard to say without more code... give us everything (and put it in the code tags, <> button on the side when you post).
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;
}
cin and cout use opposite << and >> symbols. This shouldn't even compile.

cin >> n;
cout << text;

if (n%5==0 && n%13==0);
you did it again. no ; on if statements either.
this says
if (..stuff is true) do nothing

followed by do this line regardless.

you want
if(stuff) //NO ; HERE
{
stuff to do if true
}

also, I suspect you want I, not N in your if statement.

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
putting it all together:

int main()
{
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 (i%5==0 && i%13==0)
  {
    cout << i << endl;
  }
}
return 0;
} 


which you will note is the same as I said before (%65) except this approach does much more work. Actually

for(I = 65; I <=N, I+=65)
cout << I;
is the math nerd way to do it efficiently :)

Last edited on
It works thanks so so much
I can see one problem: The first multiple of 5 and 13 after n will be printed.
Topic archived. No new replies allowed.