bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc045.c

luc045.c (1234B)


      1 /* A position integer is entered through the keyboard. Write a Function
      2 to obtain the prime factors of this number. 
      3 For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime 
      4 factors of 35 are 5 and 7
      5 */
      6 /* Let Us C, Chap- 8, Page - 144, Qn No.: C(2) */
      7 
      8 #include <stdio.h>
      9 #include <math.h>
     10 
     11 void findPrimeFactors(int n)
     12 {
     13     int temp_n = n;
     14 
     15     if (temp_n == 1)
     16     {
     17         printf("Prime factors of %d are: None.\n", n);
     18         return;
     19     }
     20 
     21     printf("Prime factors of %d are:", n);
     22 
     23     while (temp_n % 2 == 0)
     24     {
     25         printf(" %d", 2);
     26         temp_n = temp_n / 2;
     27     }
     28 
     29     for (int i = 3; i <= (int)sqrt(temp_n); i = i + 2)
     30     {
     31         while (temp_n % i == 0)
     32         {
     33             printf(" %d", i);
     34             temp_n = temp_n / i;
     35         }
     36     }
     37 
     38     if (temp_n > 2)
     39     {
     40         printf(" %d", temp_n);
     41     }
     42 
     43     printf("\n");
     44 }
     45 
     46 int main()
     47 {
     48     int n;
     49     printf("Enter a positive integer to get the prime factors: ");
     50     if (scanf("%d", &n) != 1)
     51     {
     52         printf("Error: Invalid input. Please enter an integer.\n");
     53         return 1;
     54     }
     55     if (n <= 0)
     56     {
     57         printf("Error: Please enter a POSITIVE integer.\n");
     58         return 1;
     59     }
     60     findPrimeFactors(n);
     61     return 0;
     62 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror