3 Replies - 234 Views - Last Post: 03 September 2019 - 07:11 PM Rate Topic: -----

#1 beginnerC++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • 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++   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is online

  • Code herder
  • member icon

Reputation: 7053
  • View blog
  • 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