Coders Packet

Largest Prime Factor in C++


The prime numbers that can be multiplied to give the original number are called prime factors. The largest among them is found out using C++ Programming language here.

First, we are including all the header files from the include libraries and start with the program.

To get a number from the user, 'cout' and 'cin' is used to ask and take the input as an integer number.

Next, we store it in a variable, where the prime numbers below the number are found out using a 'for loop'.

The prime number series upto the given number is found out using the function 'check_prime' in the program.

It's code is given below,

bool check_prime(int i)
  for (int iter = 2; iter <= i; iter ++)
    if (i % iter == 0 && i!=iter)
      return true;

  return false;

Using this code, it is found out whether the number is prime or not.

If the number is not a prime, it returns 'true'.

If the number is prime, it returns 'false'.

After getting the prime number series, checks out the numbers which are divisible by the given number, and the largest among them is displayed.

If the given number is also a prime number, its prime factor becomes itself.


Hope the given number works accordingly upto the interger variable memory.


Download Complete Code


No comments yet