write a C program that will accept input of two positive decimal integer values
ID: 3552164 • Letter: W
Question
write a C 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 [ ].
So you want two numbers factored.
Give them to me one by one and I will do the factoring.
Number? [12]
The prime factorization of 12 is 2*2*3
Number? [1050]
The prime factorization of 1050 is 2*3*5*5*7.
You're welcome. The bill is in the mail.
please only use if else, while loop, for loop, int ()
Explanation / Answer
// Program to print all prime factors
# include <stdio.h>
# include "math.h"
// A function to print all prime factors of a given number n
void primeFactors(int n)
{
printf("The prime factorization of n is: ");
int flag=0;
// Print the number of 2s that divide n
while (n%2 == 0)
{
if(flag==1) printf("*");
else flag=1;
printf("%d", 2);
n = n/2;
}
int i = 3;
// n must be odd at this point. So we can skip one element (Note i = i +2)
for (i; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
if(flag==1) printf("*");
else flag=1;
printf("%d", i);
n = n/i;
}
}
// This condition is to handle the case whien n is a prime number
// greater than 2
if (n > 2){
if(flag==1) printf("*");
else flag=1;
printf ("%d", n);
}
}
/* Driver program to test above function */
int main()
{
int n = 0;
printf("Enter Number: ");
scanf("%i",&n);
primeFactors(n);
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.