- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Java Program to find largest prime factor of a number

Following is the Java code to find the largest prime factor of a number −

## Example

import java.io.*; import java.util.*; public class Demo{ static long maxPrimeFactors( long val){ long max_prime = -1; while (val % 2 == 0) { max_prime = 2; val >>= 1; } for (int i = 3; i <= Math.sqrt(val); i += 2){ while (val % i == 0){ max_prime = i; val = val / i; } } if (val > 2) max_prime = val; return max_prime; } public static void main(String[] args){ int val = 148592; System.out.println("The largest prime factor of 148592 is "); System.out.println(maxPrimeFactors(val)); val = 890654; System.out.println("The largest prime factor of 890654 is "); System.out.println(maxPrimeFactors(val)); } }

## Output

The largest prime factor of 148592 is 251 The largest prime factor of 890654 is 4591

A class named Demo contains a static function that tales a value, and a ‘while’ condition is defined, which checks whether the value modulus 2 is 0. If it is 0, then, a variable (max_prime) is assigned the value 2. Otherwise, it is right bit shifted by 1. Again, a ‘for’ loop is iterated over elements from 3 to square root of the value, and incremented by 2 after every iteration.

Now, a ‘while’ loop checks if the value modulus iterator is 0. If yes, a variable (max_prime) is assigned the value that is currently being iterated. The value is divided by the iterated value. If this value is greater than 2, then this value (max_prime) is assigned to a variable named max_prime. This is returned. In the main function, an integer value is defined and the largest prime factor is found out by calling the function with the specific argument.

- Related Articles
- Python Program for Find largest prime factor of a number
- C Program for Find largest prime factor of a number?
- C Program for Find the largest prime factor of a number?
- Find largest prime factor of a number using C++.
- Finding the largest prime factor of a number in JavaScript
- k-th prime factor of a given number in java
- Java Program to find Product of unique prime factors of a number
- Java program to print a prime number
- Prime factor array of a Number in JavaScript
- Find sum of a number and its maximum prime factor in C++
- Java program to find the largest number in an array
- Prime Factor in C++ Program
- Java program to find the 3rd largest number in an array
- Java program to find the 2nd largest number in an array
- Find the largest prime number required to test as a divisor to determine if the following number is a prime number. Also determine if the number is a prime number: 117