Shamba

Prime

Jan 7th, 2021
630
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Program in C to display all the prime factors of a given positive integer in descending order.
  3.     Date : 6.01.2021
  4.     Name : Shamba Chowdhury
  5.     Roll no : 3135
  6. */
  7.  
  8. #include<stdio.h>
  9. #include<stdlib.h>
  10.  
  11. int main()
  12. {
  13.     int i, j, num;
  14.     int isPrime;
  15.  
  16.     printf("Enter the number to find the Prime factors of : ");
  17.     scanf("%d", &num);                      //Taking input of the number of which we have to find the prime factors
  18.  
  19.     if(num <= 0)
  20.     {
  21.         printf("Wrong input. Numbers less than or equal to 0 not allowed!");
  22.         exit(0);
  23.     }
  24.  
  25.     printf("\nAll the prime factors of %d in descending order are : \n", num);
  26.  
  27.     /*
  28.         Outer loop to find the factors of the given number.
  29.         The factors of a number can start from 2 and the factors are always less than half of the number
  30.     */
  31.     for(i = num/2; i >= 2; i--)
  32.     {
  33.         if(num % i == 0)
  34.         {
  35.             isPrime = 1;
  36.  
  37.             //Checking whether the factor is a prime or not
  38.             for(j = 2; j <= i/2; j++)
  39.             {
  40.                 if(i%j == 0)
  41.                 {
  42.                     isPrime = 0;
  43.                     break;
  44.                 }
  45.             }
  46.  
  47.             //Printing the number if its a factor as well as prime
  48.             if(isPrime == 1)
  49.             {
  50.                 printf("%d ", i);
  51.             }
  52.         }
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
RAW Paste Data