Prime Factors calculation

             Well, it is a very simple program to calculate the prime factors of a number using the Java programming language. The program consists of a simple logic of using prime numbers for division and then testing it as a prime factor or not. Just have a look at the code.

Program Code:

class PrimeFactors
{
  public static void main(String[] args)
  {
    int n=Integer.parseInt(args[0]);
    System.out.println("The prime factors of "+n+" are:");
    int i=2;
    while(n>1)
    {
      if(n%i==0)
      {
        System.out.print(i+" ");
        n=n/i;
      }
      else
        i++;
    }
    System.out.println();
  }
} 

Description:
               The whole program is all about using a simple logic, that first we have to conclude that the number is a prime number or not. For simplicity I have taken i variable as the first prime number 2, then increased the value till the requirement follows. One thing I must clear here that using Integer.parseInteger(args[0]) is very beginning style of taking dynamic input, which we call the command line input technique. For those who want a little bit of advanced style of input, should opt for BufferedReader or scanner. Then the input style will be a bit different.

                Now have a look at the sample output.

Sample Output:

                  Well this is it for now..! Thanks for stopping by.. I hope you have liked the program. In case if you have any query you can type it in the comment box or you can contact with me, the contact details are in my profile.
                   And please don't forget to subscribe.. The subscription is free ;)

Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java