Programming in C. 1. A positive integer is entered through the keyboard, write a
ID: 672196 • Letter: P
Question
Programming in C.
1. A positive integer is entered through the keyboard, write a C program with a function pfactors that reads an integer in, then calls the function pfactors to obtain the prime factors of the number and display them. The output of your program should look something like the below text.
Enter the integers for which the prime factors need to be found: 75 The prime factors for 75 are: 3 5 5 Would you like to run the program again? (enter Y for yes, any other letter for no): Y Enter the integers for which the prime factors need to be found: 48 The prime factors for 48 are: 2 2 2 2 3 Would you like to run the program again? (enter Y for yes, any other letter for no): q Thanks for using the program. Bye!
Explanation / Answer
# include <stdio.h>
# include <math.h>
void pfactors(int n)
{
while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
}
int i=3;
for ( i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
if (n > 2)
printf ("%d ", n);
}
int main()
{
int n,ch='Y',c;
while(ch=='Y'||ch=='y'){
printf("Enter the numbers: ");
scanf("%d",&n);
pfactors(n);
printf(" ");
scanf("%c",&c);
printf("Would you like to run the program again? (enter Y for yes, any other letter for no):");
scanf("%c",&ch);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.