In: Computer Science
Write a program that does the following. It will ask the user to
enter an integer larger than 1, and the if entered integer is not
larger than 1, it keeps prompting the user. After the user enters a
valid integer, the program prints all the prime factors of the
integer (including the repeated factors). For example, if the
entered integer is 24, the program prints:
2 2 2 3
Run your program with the test cases where the entered integers are 72 and 360360 respectively. Include your code and the screenshots of its executions in a PDF file and submit the file. Program must be in C.
Program:
#include <stdio.h>
#include <math.h>
void getPrimeFactors(int num)
{
// Print the number of 2s that divide the number
while (num%2 == 0)
{
printf("%d ", 2);
num = num/2;
}
// If the number is odd
for (int x = 3; x <= sqrt(num); x = x+2)
{
// While x divides num, print x and divide num
while (num%x == 0)
{
printf("%d ", x);
num = num/x;
}
}
// If number is prime and greater than 2
if (num > 2)
printf ("%d ", num);
}
int main()
{
int input_number;
// Read an integer input from user
printf("Enter an integer larger than 1 : ");
scanf("%d", &input_number);
// If the given number is not greater than 1 ask the user to enter number again
while (input_number <= 1) {
// Read input from user
printf("Enter an integer larger than 1 : ");
scanf("%d", &input_number);
}
// Method call to print prime factors of given number
getPrimeFactors(input_number);
return 0;
}
Output: