Primorial Number in JAVA

Primorial Number

Primorial Number is like factorial number. In Primorial Number all the prime numbers upto a given number is Multiplied.

Example : Input = 10
                  Output = 210
Primorial : 2*3*5*7

Algorithm and Program Below :-


Algorithm :-
  • Input a Number
  • check for all the prime numbers till the entered number
  • Multiply all the prime numbers in a new variable
  • Print Input Number and Primorial Number

Program :-

import java.io.*;
import java.util.*;
class Primorial
{
    public static void main(String args[])
    {
        Scanner in= new Scanner(System.in);
        int c=0,s=1;
        int n,i,j,p;
        System.out.print("Enter Number: ");
        n= in.nextInt(); // Input a Number
        p=n; // copy of input number
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=i;j++)
            {
                if(i%j==0) // checking prime number
                {
                    c++;
                }
            }
            if(c==2)
            {
                s=s*i; // Multiplying Prime numbers
            }
            c=0; // Initializing c=0 to check for next prime number
        }
        System.out.println("Original Number : "+p);
        System.out.println("Primorial Number : "+s);
    } // end of main
} // end of class


All the best :)

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

Tri-Automorphic Number in JAVA