Page 1 of 1

9 Replies - 10386 Views - Last Post: 08 February 2010 - 09:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=154048&amp;s=372f80a6873ce473bac7f0ce48a39d80&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 solo321

Reputation: 0
• Posts: 7
• Joined: 05-February 10

Posted 05 February 2010 - 12:22 AM

Hello,

I am trying to develop this code in C++ where the user inputs an integer, such as 10. And the program finds all the divisors between 1 and the number the user inputed such as 10.

The program will output the answer to the user in a fashion like this:

list of divisors of the integer between 1 and 10:
>divisors of 1: 1
>divisors of 2: 1, 2
>divisors of 3: 1, 3
>divisors of 4: 1, 2, 4
>divisors of 5: 1, 5
and so on.

I have started to write a code for this, and I think I have done a decent job laying out the easy outline part. I just need some help developing an algorithm for it.

What I have so far is below, any advice is greatly appreciated.

```int main()
{
//Declare Varibles
int val,count,div

//Get input from user

cout >> "Please enter the maximum integer value you wish to find divisors:">>endl;
cin >> val;

//Execute commands

count = 1;
cout>> "List of divisors of the the integer between 1 and ">>val>>endl;

while(count=<val)
{
count%count = div;
cout>>"divisors of ">>count>>":">>div>>endl;
count++;
}

```

This post has been edited by NickDMax: 05 February 2010 - 07:39 AM

Is This A Good Question/Topic? 0

Replies To: Finding divisors code advice

#2 Martyn.Rae

• The programming dinosaur

Reputation: 545
• Posts: 1,420
• Joined: 22-August 09

Posted 05 February 2010 - 12:40 AM

You will need to write a loop that varies an integer from 1 to the number the user has input. Use the modulus not the division operator when you divide the number entered by the user with the varying integer. If the result is zero, then that is one of divisors.

Example:

```   int divisor;

for ( divisor = 1; divisor < user_value; divisor++ ) {
if ( user_value%divisor == 0 ) {
/* we have success on this divisor */
}

```

Hope that helps.

#3 seeP+

Reputation: 55
• Posts: 601
• Joined: 20-July 09

Posted 05 February 2010 - 12:42 AM

Just an algorithm? You have many things wrong with the code you just posted.

#4 Munawwar

• D.I.C Regular

Reputation: 163
• Posts: 457
• Joined: 20-January 10

Posted 05 February 2010 - 12:46 AM

```cout >> "Please enter the maximum integer value you wish to find divisors:">>endl;
cin >> val;

```

It's supposed to be cout<<"whatever";...you got the << operator wrong.When I learnt C++ I always kept few points about cout in mind.cout means 'console output'...imageine << like an arrow...the string "whatever" goes into the console.

```while(count=<val)
{
count%count = div; //Two errors here
...
...

```

div should be on the left hand side. Thats div=count%count.This sytax is correct.
But hey! There is a logical error here. count%count will always give you zero. (That is, the reminder of count divided by count.Any number divided by itself will give you 1 and the reminder is zero.) Think about it.

#5 solo321

Reputation: 0
• Posts: 7
• Joined: 05-February 10

Posted 05 February 2010 - 03:33 PM

seeP+, on 04 February 2010 - 11:42 PM, said:

Just an algorithm? You have many things wrong with the code you just posted.

I know, i just wrote the code up. I did not have time to debug it. I will post the finished code, or what I have improved on it soon.

#6 solo321

Reputation: 0
• Posts: 7
• Joined: 05-February 10

Posted 08 February 2010 - 02:47 PM

Hello,

I am trying to develop this code in C++ where the user inputs an integer, such as 10. And the program finds all the divisors between 1 and the number the user inputed such as 10.

The program will output the answer to the user in a fashion like this:

list of divisors of the integer between 1 and 10:
>divisors of 1: 1
>divisors of 2: 1, 2
>divisors of 3: 1, 3
>divisors of 4: 1, 2, 4
>divisors of 5: 1, 5
and so on.

I have started to write a code for this, and I think I have done a decent job laying out the easy outline part. I just need some help developing an algorithm for it. (I know I may have some debugging errors. and what not)

What I have so far is below, any advice or help is greatly appreciated.

```#include <iostream>

using namespace std;

int main()
{
//Declare Varibles
int val,count,div;

//Get input from user

cout << "Please enter the maximum integer value you wish to find divisors:" << endl;
cin >> val;

//Execute commands
{
count = 1;
cout << "List of divisors of the the integer between 1 and " << val << endl;
{

for (div = 1; div < val; div++)
if (div = (val%div == 0))
count++;

while (div <= val); //div is no larger than val
}
cout << "Divisors of " << count << ": " << div << endl;
}

}
```

#7 taylorc8

• B&

Reputation: 150
• Posts: 1,572
• Joined: 21-July 09

Posted 08 February 2010 - 06:13 PM

iterate though each number using the % (modulus) operator to check if there's a remainder... I assume your teacher means EVENLY divisible and less than the number... if the % returns anything but zero there is a remainder after division meaning the number is NOT evenly divisible. How hard can it be? if(!(10%3)) // if it's zero, do something...

#8 solo321

Reputation: 0
• Posts: 7
• Joined: 05-February 10

Posted 08 February 2010 - 09:30 PM

My compiler is not working correctly at the moment so I need to wait till I am at school to test this code. I thought I did tell the //if 0 do something when I stated if (div = (val%div == 0)). I made the if 0 the int div and now can use it in cout <<. How do I display multiple answers for this? I just need some clarity on finishing this code. Thanks

#9 solo321

Reputation: 0
• Posts: 7
• Joined: 05-February 10

Posted 08 February 2010 - 09:34 PM

Ok so I did some more messing around with this code and got this:

#include <iostream>

using namespace std;

int main()
{
//Declare Varibles
int val,count,div;

//Get input from user

cout << "Please enter the maximum integer value you wish to find divisors:" << endl;
cin >> val;

//Execute commands
{
count = 1;
cout << "List of divisors of the the integer between 1 and " << val << endl;
{

for (div = 1; div < val; div++)
if (div = (val%div == 0))
count++;

while (div <= val); //div is no larger than val
}
cout << "Divisors of " << count << ": " << div << endl;
}

}

I labled what the answer would be as div when I did the (div = (val%div == 0)). I just dont know how to display multiple answers for the same "div" varible. I am kinda lost of what to do next. Help on this? And thank you for your responses they have all helped.

#10 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

Posted 08 February 2010 - 09:38 PM

Something along these lines:

```...
cin >> val; //number to go to

for(int i = 1; i < val; i++){
cout << "Divisors of: " << i << " ";
for(int j = 1; j < val; j++){ //inefficient, just an example
if (val % j == 0) cout << j << " "; //output divisors
}
}

```