Im trying to get a program that reads in a positive integer and prints out all the factors of that integer. I dont know how to word the code to do the division and only print out the factors. Any help would be greatly appreciated.
Im trying to get a program that reads in a positive integer and prints out all the factors of that integer. I dont know how to word the code to do the division and only print out the factors. Any help would be greatly appreciated.
Start with: how do you know that an integer is a factor of the given integer?I dont know how to word the code to do the division and only print out the factors.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
by seeing if the division gives a whole number
So if I gave you (or your program) 148, what should you (or your program) print? All the possible factors, all the factors paired off, all the prime factors, something else? Once you know what the answers are, you'll know how to do it.
Take a look at the modulus (% operator. For example if you were given the input number 6, and did a loop from 1 to that value(6), and did something like this
Code:Make sure that the input number is greater than 1, otherwise this is trivial. Test it to make sure. for (int i = 1; i <= value; i++) { remainder = value % i; if (remainder == 0) { factor1 = i; factor2 = value / i; printf("FACTOR 1 = %d\n, factor1); printf("FACTOR 2 = %d\n, factor2); } }
well i would divide it with every number between 1 and its self.. so would I set up a loop to keep dividing and then -- for the next number down?
so I am starting with this and i get like 15 errors
Code:#include <stdio.h> int main (void){ int i,value,factor1,factor2,remainder; for (int i=1; i<= value; i++) remainder = value % i; if (remainder == 0) { factor1 = i; factor2 = value / i; printf("FACTOR 1 = %d\n, factor1); printf("FACTOR 2 = %d\n, factor2); } } system("pause"); return 0 }
Well, yeah, I suppose: the body of your for-loop is not in {curly braces}, you don't have any close quotes in your printf statements, you don't have a semicolon after your return statement, you can't declare new variables inside the parentheses of a for() statement, and even if you could you've already got an integer named i. And, system is in the stdlib library, so you would need to include that header file too. I think that's it.
You only need to test up to half the value since nothing greater can possibly divide it evenly.
And you only really need odd numbers (and test 2 separately).
Ok all that is running smoothly now added in some print and scan statements at beginning. How do I get it to not repeat any numbers?
for example when run with 10Code:#include <stdio.h> int main (void){ int i,value,factor1,factor2,remainder; printf("Please enter the inter you want to factor.\n"); scanf("%d",&value); for (i=1; i<=value; i++) { remainder = value % i; if (remainder == 0) { factor1 = i; factor2 = value / i; printf("FACTOR 1 = %d\n", factor1); printf("FACTOR 2 = %d\n", factor2); } } system("pause"); return 0; }
i get
1
10
2
5
5
2
10
1
Actually, to find all factors or pairs of factors one only needs to test to the square root of the value.You only need to test up to half the value since nothing greater can possibly divide it evenly.
Not true, since multiples of two can also be valid factors (e.g., 12 = 2 * 6, but also 3 * 4).And you only really need odd numbers (and test 2 separately).
Of course, this is assuming that clipsit wants to find all factors or pairs of factors instead of the prime factors. clipsit, you need to be specific about this requirement.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
only guideline is "Create a list of each of the positive factors of the given integer"
Ah.
As I noted, you need to terminate the loop once you reach the square root of the value. This happens when factor1 >= factor2, so you just need to keep looping while factor1 < factor2.How do I get it to not repeat any numbers?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
sorry Im still really new how do i terminate the loop. I set up the if statement but dont know where to go from there.
Either using the loop condition or with a break.how do i terminate the loop
What is your current code? Good to see that you are using code tags, but also try and indent your code consistently.I set up the if statement but dont know where to go from there.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)