Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lab02 due In class we have written a program that does several prime number rela

ID: 667961 • Letter: L

Question

Lab02 due In class we have written a program that does several prime number related things. For this lab you are asked to write a function of type void named "factors that takes one argument (an integer) and prints on the terminal the prime numbers decomposition of that number. The void type just means the function is not returning anything. So the function factors" does all the necessary printing and does not return anything. To declare the factors function as void, just write void factors (int a) //Yi our code here return Note that retum, here, is not followed by anything The format of the displayed result is specific and is illustrated by the following examples your function should reproduce: 1697025 3 x 5 2 x 11 3 x 17 23 23 81 3 4 2093 7 x 13 x 23 9075 3 x 5 2 x 11 2 In particular you want to have a only when the power is not 1 and you want to have a 'x' in front of all the factors except the first one Do not forget to enter comments in your code so we can understand how it works, making us more inclined to giving you the credit. Submit your lab02 work using the conmand. submit p6720 Lab02 intro. cpp

Explanation / Answer

void factors(int n)
{
int i,j;
for(i=2;i<=n;i++)
{
if(n%i==0)
{
j=0;
while(n%i==0)
{
n=n/i;
j++;
}
if(j==1)
printf("%d x ",i);
else
printf("%d^%d x ",i,j);
}
}
}

OR

void factors(int n)
{
int i,j;
for(i=2;i<=n;i++)
{
if(n%i==0)
{
j=0;
while(n%i==0)
{
n=n/i;
j++;
}
if(n==1)
{
if(j==1)
printf("%d ",i);
else
printf("%d^%d ",i,j);
}
else
{
if(j==1)
printf("%d x ",i);
else
printf("%d^%d x ",i,j);
}
}
}

}