zach101 0 Report post Posted October 25, 2005 Guys some of these exercises in my book i just tottaly dont get and this is basically a self taught class so im trying my best to learn just out of the book. But there are a few problems in paticular i need help with such as these prob 1. should do/look like thisEnter a number: 140 // so the 140 is cin >> num;Prime factors: 2,2,5,7 // so it displays the prime factors of the numberNow if some one could tell me how to do that i would be so appreciated some people said do a do while while others said a for loop basically im just tottaly lost any help would be awesome. Thanks guys Share this post Link to post Share on other sites
sangdukseo 0 Report post Posted October 25, 2005 do-while and for loops pretty much do the same thing so it doesn't matter which one you use. I think what they mean is that if you want to find the primes of 140 for example, the first time you run through your loop you would get say, 70 and 2. Within the loop, you have an if-then statement that says that if any prime #'s come up, store it within an array that uses your counter for your loop as it's location in the array. That stores 2. The next time the loop runs, 70 splits up into 35 and 2. The array stores 2 and the loop runs again. This continues until you only have primes. The loop ends, and you tell the program to cout << array and put the command inside a for loop using the same counter that you used for your loop above. I hope this helps. Maybe I should have just written out the program. But this is the logic involved. Share this post Link to post Share on other sites
zach101 0 Report post Posted October 26, 2005 Okay im really sorry im buging you guys again but i seriously need help in this C++ thing cause im currently takign VB at the same time.... I guess i should of taken VB first eh? cause this c++ class is basically my intro into any kind of programming and its pretty hard for me so please bare with my newb self. Okay heres the code i have writen so far of course it has erros but i feel like im at least a little closer and thanks sang that reply did help but could u point out whats screwing the code up this time? /* Exercise 12Everman, Zach Oct 26 05 */#include<iostream.h>int main(){int num;int divisor;cout << "Enter a number";cin >> num;divisor=2;while (divisor <= num) {divisor = divisor + 1;}if (num / divisor != 0)cout << divisor;elsecout << " no Prime factors"; }return(0);} Share this post Link to post Share on other sites
mama_soap 0 Report post Posted November 1, 2005 Ok, a couple of things. First, you have: while (divisor <= num) {divisor = divisor + 1;} That's an extra closing bracket. When this code is compiled, the compiler will give the variable "divisor" some location in the memory and dump the number "2" there. Then it will excecute this while loop and by the time it is done, that location will have the value of "num", and the rest of the code is rendered pointless. if (num / divisor != 0)cout << divisor;elsecout << " no Prime factors"; Okay, the num/divisor should be num%divisor. You only want to know if divisor divides evenly into num or not. For that, you only need to check on the remainder, not the quotient. Also, this will output those divisors also which are non-primes. For instance, if num=8, the program will probably output 2,4,8, of which 4 and 8 are definitely composite. I'd suggest you write a small isprime() function and check if divisor is prime or not during every iteration. The code will look like this: if(num % divisor != 0){ if(isprime(divisor)) cout << divisor;} Again, the else statement should be outside the while loop, not inside it. If it's inside, your output will resemble: 2no prime factors4no prime factorsno prime factorsno prime factors8 which is clearly not desirable. Please remember to write out the function isprime() in full. It's a standard function you'll find in any text, and besides, it's also a good exercise if you're starting out Good luck and keep us updated. Share this post Link to post Share on other sites
sharpz 0 Report post Posted November 16, 2005 you could try factoring first. add a loop to check all integers beetween 1 and the number are evenly divisible. use the modulus operator ( % ) to check if there is a remainder or not when you divide. if you look on google, you can see many examples of prime numbers and factoring in C++ such as http://www.dreamincode.net/forums/topic/13192-prime-number/ orhttp://forums.xisto.com/no_longer_exists/ Share this post Link to post Share on other sites
baseballkid781 0 Report post Posted November 22, 2005 (edited) spam Edited November 3, 2016 by OpaQue (see edit history) Share this post Link to post Share on other sites