Extracting Divisors: A Simple Guide for Adding Factors

  • Thread starter soul5
  • Start date
In summary, the conversation discusses extracting divisors from a given number and adding them together. The suggested solution involves using an if statement to check if a number is a divisor, declaring an integer to store the sum of divisors, and using a for loop to iterate through the possible divisors. It is also suggested to specify the for loop as i<=n/2 for improved efficiency.
  • #1
soul5
64
0
Ok this is what I did


int n;
cout << enter number;
cin >> n;

for (int i = 1 ; i < n ; i++ )

if ( n % i == 0 )



I could use some help please like how to I extract the divisor?

so I can add them. Thanks a lot.
 
Last edited:
Technology news on Phys.org
  • #2
Your if condition tells you if i is a divisor. Since you said you want to add up all the divisors you have to declare an identifier, say int sum=0; at the start.

So you can do this should the if statement be satisfied:

if(n%i==0)
sum += i;

If you're writing a function, then you'll want to return sum after you're done. Alternatively if you want all the divisors individually, one way is to initialise an integer vector, say vector<int> divisors (remember to #include <vector>), and use divisors.push_back(i); each time your if condition is true.

More importantly you can improve your program slightly by specifying your for loop as :
for(int i=1;i<=n/2;i++) since it is evident that there cannot be any divisors of n which is larger than n/2.
 
Last edited:
  • #3
soul5 said:
int sum=0;
int n;
cout << enter number;
cin >> n;

for (int i = 1 ; i <= n ; i++ ) {

if ( n % i == 0 )

sum += i;

}

cout << " sum is " << sum << endl;

changed again
 
Last edited:
  • #4
Well it looks ok to me. You forgot a semiconlon for your sum statement though. The best way to know if it'll work is to actually try it out. One thing overlooked earlier: your for loop doesn't allow i to be "n", which it should, since for any number n, n itself is a divisor of n. So change it to i<=n.
 

1. What is a divisor?

A divisor is a number that divides evenly into another number, resulting in a whole number without any remainder. For example, 3 is a divisor of 9 because 9 ÷ 3 = 3 with no remainder.

2. Why do I need to extract a divisor?

Extracting a divisor can be useful in solving mathematical problems or simplifying fractions. It allows us to break down a larger number into smaller, equal parts.

3. How do I find all the divisors of a given number?

To find all the divisors of a number, you can start by listing all the factors of the number. Then, you can divide the number by each factor to find the corresponding divisor. For example, the divisors of 12 are 1, 2, 3, 4, 6, and 12.

4. Can a number have more than one divisor?

Yes, a number can have multiple divisors. In fact, every number has at least two divisors - 1 and itself. For example, 12 has 6 divisors - 1, 2, 3, 4, 6, and 12.

5. Is 1 a divisor of every number?

Yes, 1 is a divisor of every number. This is because 1 can be divided into any number without any remainder. However, 1 is not considered a proper divisor because it is the number itself.

Similar threads

  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
Replies
10
Views
960
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
5
Views
884
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top