luc045.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 12 Dec 2025
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
A position integer is entered through the keyboard. Write a Function to obtain the prime factors of this number. For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <math.h>
void findPrimeFactors(int n)
{
int temp_n = n;
if (temp_n == 1)
{
printf("Prime factors of %d are: None.\n", n);
return;
}
printf("Prime factors of %d are:", n);
while (temp_n % 2 == 0)
{
printf(" %d", 2);
temp_n = temp_n / 2;
}
for (int i = 3; i <= (int)sqrt(temp_n); i = i + 2)
{
while (temp_n % i == 0)
{
printf(" %d", i);
temp_n = temp_n / i;
}
}
if (temp_n > 2)
{
printf(" %d", temp_n);
}
printf("\n");
}
int main()
{
int n;
printf("Enter a positive integer to get the prime factors: ");
if (scanf("%d", &n) != 1)
{
printf("Error: Invalid input. Please enter an integer.\n");
return 1;
}
if (n <= 0)
{
printf("Error: Please enter a POSITIVE integer.\n");
return 1;
}
findPrimeFactors(n);
return 0;
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55