Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program in C to display all the prime factors of a given positive integer in descending order.
- Date : 6.01.2021
- Name : Shamba Chowdhury
- Roll no : 3135
- */
- #include<stdio.h>
- #include<stdlib.h>
- int main()
- {
- int i, j, num;
- int isPrime;
- printf("Enter the number to find the Prime factors of : ");
- scanf("%d", &num); //Taking input of the number of which we have to find the prime factors
- if(num <= 0)
- {
- printf("Wrong input. Numbers less than or equal to 0 not allowed!");
- exit(0);
- }
- printf("\nAll the prime factors of %d in descending order are : \n", num);
- /*
- Outer loop to find the factors of the given number.
- The factors of a number can start from 2 and the factors are always less than half of the number
- */
- for(i = num/2; i >= 2; i--)
- {
- if(num % i == 0)
- {
- isPrime = 1;
- //Checking whether the factor is a prime or not
- for(j = 2; j <= i/2; j++)
- {
- if(i%j == 0)
- {
- isPrime = 0;
- break;
- }
- }
- //Printing the number if its a factor as well as prime
- if(isPrime == 1)
- {
- printf("%d ", i);
- }
- }
- }
- return 0;
- }
RAW Paste Data