LARGEST PRIME FACTOR OF A NUMBER


package com.cnc;

public class Main {

public static void main(String[] args) {
System.out.println(getLargestPrime(20));
System.out.println(getLargestPrime(7));
}
public static int getLargestPrime(int number){
int i,stored=number;

if(number<=0)
return -1;
else if(isPrime(number)){
number=stored;
}

else{
for( i = 2; i < number; i++){
if(number%i==0){
stored=i;

}
}
}
return stored;
}
public static boolean isPrime(int n){
for(int j =2;j<n;j++){
if (n%j==0) return false;
else return true;
} return false;
}
}
Here i created a new method isTrue. if the number is prime then the number will return the
same as output. but if it is not a prime then it will return the largest number excluding
itself.
eg: 20--->10
7---->7



Popular posts from this blog

NUMBER PALINDROME CHALLENGE

ENCAPSULATION CHALLENGE

USER INPUT PRINT ORIGINAL,REVERSE ARRAY AND SORTED ARRAY