171k views
1 vote
Write a program that will accept input of two positive decimal integer values and output the prime factorizations of the input values. A sample run is below. User input is in boldface. So you want two numbers factored. Give them to me one by one and I will do the factoring.

A. The prime factorization of 12 is:________.
B. The prime factorization of 1050 is:________.

1 Answer

4 votes

Answer:

# include <stdio.h>

# include "math.h"

void primeFactors(int n)

{

printf("The prime factorization of n is: ");

int flag=0;

while (n%2 == 0)

{

if ( flag == 1 ) printf("*");

else flag=1;

printf("%d", 2);

n = n/2;

}

int i = 3;

for (i; i <= sqrt(n); i = i+2)

{

while (n%i == 0)

{

if(flag==1) printf("*");

else flag=1;

printf("%d", i);

n = n/i;

}

}

if (n > 2){

if(flag==1) printf("*");

else flag=1;

printf ("%d", n);

}

}

int main()

{

int n = 0;

printf("Enter Number: ");

scanf("%i",&n);

primeFactors(n);

printf("\\");

return 0;

}

Step-by-step explanation:

  • Create a function to print all prime factors of a given number n
  • Print the number of 2s that divide n and n must be odd at this point. So we can skip one element.
  • While i divides n, print i and divide n
  • Apply condition to handle the case when n is a prime number.
User Mohamed Helmy
by
5.1k points