Find the Largest Prime Factor in Java

Introduction 

 
The code snippet in this blog is an example of how to find the largest prime factor from a given number in Java. 
  1. package largestprimefactor.FreeSampleProjects.com;  
  2. import java.util.Scanner;  
  3. public class LargestPrimeFactor {  
  4.  public static void main(String[] args) {  
  5.   Scanner input = new Scanner(System.in);  
  6.   System.out.println("Enter a number?");  
  7.   int number = input.nextInt();  
  8.   int result = largestPrimeFactor(number);  
  9.   System.out.println("The result is " + result);  
  10.  }  
  11.  public static int largestPrimeFactor(int n) {  
  12.   if (n <= 1) {  
  13.    return 0;  
  14.   } else {  
  15.    int div = 2;  
  16.    while (div < n) {  
  17.     if (n % div != 0) {  
  18.      div++;  
  19.     } else {  
  20.      n = n / div;  
  21.      div = 2;  
  22.     }  
  23.    }  
  24.    return n;  
  25.   }  
  26.  }  
  27. }   
Next Recommended Reading Java Program To find HCF And LCM