Here is a C++ problem: It is a well-known mathematical fact that every integer g
ID: 3677347 • Letter: H
Question
Here is a C++ problem:
It is a well-known mathematical fact that every integer greater than or equal to 2 has a unique factorization into prime numbers. For example:
40 = 2 * 2 * 2 * 5
58 = 2 * 29
9 = 3 * 3
30 = 2 * 3 * 5
100 = 2 * 2 * 5 * 5
Write a recursive function:
void printPrimeFactorization(int n, bool firstcall);
that prints out the prime factorization of the number n. (You will need to figure out for yourself the purpose of the boolean argument firstcall.)
For example, if n == 40, it should print out:
The prime factorization of 40 is 2 * 2 * 2 * 5.
Explanation / Answer
#include<iostream>
int main()
{
cout << "Enter a number: ";
int num;
cin >> num;
int i,k = 0;
for (int i=2; i <= num; i++)
{
if( num % i == 0)
{
if(num % i == 0)
{
k = num / i; } cout << i << " " << k << endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.