Menu
Topics Index
...
`

Calculate The Highest Prime Factor of A Number


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to get the highest prime factor of a number.

Input(Number) Output(Number)
19 19
50 5
111 37



class HighestPrimeFactor
{
    public static void main(String s[])
    {
        int inputNumber = 209;
        int primeFactor = highestPrimeFactor(inputNumber);
        System.out.println("The factor is " + primeFactor);

    }


    public static int highestPrimeFactor(int input)
    {
    }
    public static boolean isPrime(int number)
    {
    }
    
    public static int[] getFactors(int number)
    {
    }

}
Topic: Learn Arrays And Loops

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

here we need to find the highest prime factor, start finding the factors from reverse means highest to lowest..
for(int i=num;i>=1;i--)
* now if i is factor of the num than pass that  number to isPrime() if it is true than return that number

Posted by Uday Kumar    2015-03-12 08:05:01


To get highest prime factor of a number we have to define 3functions:

1.get factors() in which we find factors of a input number.
2.is prime() in which we check if number is prime or not.
3.highest factor() in which we find highest number from the found out prime factors.

1.get factors():
To get factors of number we divide number by all the numbers from 1 to 1 less than that number and if remainder comes out to be zero then then we check if it is prime or not susing method() 2.

First of all get highest prime factor() is called.from that method we have to call get factors() method and get the prime factors as follows:

Int j=0;
For( int I=1;I<input;I++)
{
If(Input%i==0)
{
If(is prime(I)==true)
{
Arr[j]=i;
J++;
}
}
}
Return Arr;//returning array of prime factors to get highest prime factor() method.

---Mow in that method we have to find largest number by saving first indexed 0 element to temp and by comparing every element of array to that number if it is smaller than we have to store larger number in temp.

We have to return temp to main() method.

Posted by Mânïshå Mùlchåndânï    2015-03-12 08:25:02


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Second Correct Comment'. The 'lucky liker' is Dheeraj Grewal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-13 02:14:29


© meritcampus 2019

All Rights Reserved.

Open In App