Finding divisors code advice

Need some help getting to the next step in this code

Page 1 of 1

9 Replies - 5944 Views - Last Post: 08 February 2010 - 09:38 PM Rate Topic: -----

#1 solo321  Icon User is offline

  • New D.I.C Head

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

Finding divisors code advice

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++;
          }




Thank you in advance

This post has been edited by NickDMax: 05 February 2010 - 07:39 AM
Reason for edit:: Added/Fixed code tags [code]...your program here...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: Finding divisors code advice

#2 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Re: Finding divisors code advice

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.
Was This Post Helpful? 1
  • +
  • -

#3 seeP+  Icon User is offline

  • D.I.C Addict

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

Re: Finding divisors code advice

Posted 05 February 2010 - 12:42 AM

Just an algorithm? You have many things wrong with the code you just posted.
Was This Post Helpful? 0
  • +
  • -

#4 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: Finding divisors code advice

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.
Was This Post Helpful? 1
  • +
  • -

#5 solo321  Icon User is offline

  • New D.I.C Head

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

Re: Finding divisors code advice

Posted 05 February 2010 - 03:33 PM

View PostseeP+, 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.
Was This Post Helpful? 0
  • +
  • -

#6 solo321  Icon User is offline

  • New D.I.C Head

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

Re: Finding divisors code advice

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;
        }



}



Thank you in advance
Was This Post Helpful? 0
  • +
  • -

#7 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Finding divisors code advice

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...
Was This Post Helpful? 0
  • +
  • -

#8 solo321  Icon User is offline

  • New D.I.C Head

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

Re: Finding divisors code advice

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
Was This Post Helpful? 0
  • +
  • -

#9 solo321  Icon User is offline

  • New D.I.C Head

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

Re: Finding divisors code advice

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.
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Finding divisors code advice

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 
    }
}



*Threads merged* Please do not create duplicate threads on the same problem.

This post has been edited by KYA: 08 February 2010 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1