C Program to Find Largest Prime Factor

The following program will help you to find the largest prime factor of a number. If you want to know know about prime factors you may visit this post by me.

Here is the C Program:


#include <stdio.h>

int main()
{
    long long  number;
    printf("Enter a number to know its largest prime factor: ");
    scanf("%lld",&number);
    long long div=2, ans = 0, maxFact;
    while(number!=0){
        if(number % div !=0)
            div = div + 1;
        else{
            maxFact  = number;
            number = number / div;

            if(number == 1){
                printf("%d is the largest prime factor !",maxFact);
                ans = 1;
                break;
            }
        }
    }
    return 0;
}

0 comments :

Post a Comment

Spam comments will be deleted. :)

 
Loading...
TOP