can you help please. include a Word document with all parts (all source code, fl
ID: 642440 • Letter: C
Question
can you help please.
include a Word document with all parts (all source code, flowchart or UML diagram, screen shots of output)
Prime Factors
The prime factors of a number are all of the prime numbers that can be multiplied together to get the number.
Examples:
8=1*2*2*2
3=1*3
10=1*2*5
24=1*2*2*2*3
Your program should find the prime factors recursively. Find the first factor by trying the divisors 2,3,5,7,11,13, 15 (15 won't work because we already tried 3 and 5, but it is easier to count by 2's) . If no divisor works up to the square root, then the number is prime.
Lets see how we got the results for 24.
The user entered 24 and we printed "24=1*"
We call factors(24). In factors(24) we find the divisor 2 and print "2*" and then call factors(12).
In factors(12) we find the divisor 2 and print "2*" and then call factors(6).
In factors(6) we find the divisor 2 and print "2*" and then call factors(3).
In factors(3) we cannot find a divisor so we print "3" and end
Explanation / Answer
#include <iostream>
using namespace std;
void factors(int num){
int i = 2;
while(num % i != 0){
++i;
}
if(num == i){
cout << i << endl;
return;
}
cout << i << " * ";
return factors(num / i);
}
int main(){
int num;
cout << "Enter a number: ";
cin >> num;
cout << num << " = 1 * ";
factors(num);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.