FOR LOOP. (HELP PLEASE)

Page 1 of 1

3 Replies - 234 Views - Last Post: 03 September 2019 - 07:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417218&amp;s=ef0aaa6ddc55e3f0175975fd4dc095dc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 beginnerC++

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 14-August 19

FOR LOOP. (HELP PLEASE)

Posted 03 September 2019 - 06:48 PM

```#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int num1,num2;
int fnd=0,ctr=0;

cout << " Find integers divisible by 17 or 19 " << endl;

cout << " Input number for starting range: ";
cin>> num1;

cout << " Input number for ending range: ";
cin>> num2;

cout << "The divisible integers by 17 or 19 between " << num1 << "and " << num2 <<" are : " << endl;

for(int i=num1;i<=num2;i++)

{
for(int j=2;j<=sqrt(i);j++)
{
if(i%j==0)
ctr++;
}
if(ctr==0&&i!=1)

{ fnd++;
cout<<i<<" ";
ctr=0;
}
ctr=0;
}

cout<<"\n\n The total number between " << num1 << " to " << num2 << " is : " << fnd <<endl;

return 0;

}
```

... So, this code works, but it took me hours to figure it out how to do it. I watched lots of videos and read my book . For some of you might look pretty easy. So my question is, do you have have another way to solve it , maybe easier or shorter ? Thank you in advance.

Is This A Good Question/Topic? 0

Replies To: FOR LOOP. (HELP PLEASE)

#2 modi123_1

• Suitor #2

Reputation: 15262
• Posts: 61,154
• Joined: 12-June 08

Re: FOR LOOP. (HELP PLEASE)

Posted 03 September 2019 - 07:03 PM

Please explain what you are attempting to do there. Some sample input, and output.
Was This Post Helpful? 0

#3 beginnerC++

• New D.I.C Head

Reputation: 0
• Posts: 9
• Joined: 14-August 19

Re: FOR LOOP. (HELP PLEASE)

Posted 03 September 2019 - 07:06 PM

@modi123_1 So the question says: Find the integers divisible by 17 or 19 in the range from 100 to 200 . In that code , i just realized it displays the numbers but they are wrong because none are divisible by 17 or 19.. So, i am back how I started.
Was This Post Helpful? 0

#4 Skydiver

• Code herder

Reputation: 7053
• Posts: 23,974
• Joined: 05-May 12

Re: FOR LOOP. (HELP PLEASE)

Posted 03 September 2019 - 07:11 PM

Are you supposed to just count the number of integers that are divisible by 17 or 19, or are you suppose to list them? It sometimes makes a difference.

Also why are you essentially doing a prime number check on lines 22-26? You don't need that if all you need to do is check for divisibility by 17 or 19.

Here's simple pseudo-code of what needs to be done:
```input range start
input range end
for each integer n within the range do:
if (n mod 17 == 0) or (n mod 19 == 0)
output n

```

Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }