Home > Backend Development > C++ > body text

C program to find the largest prime factor of a number

王林
Release: 2023-08-27 10:09:05
forward
1517 people have browsed it

C program to find the largest prime factor of a number

Prime Factor− In number theory, the prime factors of a positive integer are the prime numbers that divide that integer exactly. The process of finding these numbers is called integer factorization, or prime factorization.

Example− Prime factors of 288 are: 288 = 2 x 2 x 2 x 2 x 2 x 3 x 3

Input: n = 124
Output: 31 is the largest prime factor!
Copy after login

Explanation

You will find all the prime factors of a number and find the largest prime factor among them. The prime factors of 124 are 2 x 2 x 31, where 31 is the largest prime factor.

Example

#include <stdio.h>
int main() {
   long int n;
   n=3453;
   long int div=2, ans = 0, maxFact;
   while(n!=0) {
      if(n % div !=0)
         div = div + 1;
      else {
         maxFact = n;
         n = n / div;
         if(n == 1) {
            printf("%d is the largest prime factor !",maxFact);
            ans = 1;
            break;
         }
      }
   }
   return 0;
}
Copy after login

Output

1151 is the largest prime factor !
Copy after login

The above is the detailed content of C program to find the largest prime factor of a number. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template