luc045.c (1425B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 12 Dec 2025 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* A position integer is entered through the keyboard. Write a Function 9 to obtain the prime factors of this number. 10 For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime 11 factors of 35 are 5 and 7 12 */ 13 /* Let Us C, Chap- 8, Page - 144, Qn No.: C(2) */ 14 15 #include <stdio.h> 16 #include <math.h> 17 18 void findPrimeFactors(int n) 19 { 20 int temp_n = n; 21 22 if (temp_n == 1) 23 { 24 printf("Prime factors of %d are: None.\n", n); 25 return; 26 } 27 28 printf("Prime factors of %d are:", n); 29 30 while (temp_n % 2 == 0) 31 { 32 printf(" %d", 2); 33 temp_n = temp_n / 2; 34 } 35 36 for (int i = 3; i <= (int)sqrt(temp_n); i = i + 2) 37 { 38 while (temp_n % i == 0) 39 { 40 printf(" %d", i); 41 temp_n = temp_n / i; 42 } 43 } 44 45 if (temp_n > 2) 46 { 47 printf(" %d", temp_n); 48 } 49 50 printf("\n"); 51 } 52 53 int main() 54 { 55 int n; 56 printf("Enter a positive integer to get the prime factors: "); 57 if (scanf("%d", &n) != 1) 58 { 59 printf("Error: Invalid input. Please enter an integer.\n"); 60 return 1; 61 } 62 if (n <= 0) 63 { 64 printf("Error: Please enter a POSITIVE integer.\n"); 65 return 1; 66 } 67 findPrimeFactors(n); 68 return 0; 69 }