Tuesday, June 4, 2013

Program to find out Primorial(P#) of a number

PROGRAM
Primorial(P#) is defined to be the product of prime numbers less than or equal to P. e.g.
3# = 2*3=6
5# = 2*3*5=30
13# = 2*3*5*7*11*13=30030
This is also called p-prime-factorial.
WAP to input a number and print its primordial i.e. n#
SAMPLE DATA
Enter a number=13
13#=2*3*5*7*11*13=30030
---------------------------------------------------------------------------------------------------------------------

/**
 * class primorial finds the primorial of a given number.
 * @author: Nitendra Kumar
 * @version: June 4/2013
 */
import java.util.Scanner;
public class primorial
{
  public int num=0;
  public void getnum()  //method to accept number from user
  {
      Scanner in=new Scanner(System.in);  //creating object of Scanner class
      System.out.println("Enter a number");
      num=in.nextInt();
    }
    public void findprimorial()  //method to find out primorial
    {
        int res=2,flag=0;
        String str="2";
        for(int i=3;i<=num;i++)
        {
         flag=0;
        for(int j=2;j<i;j++)
        {
           if(i%j==0)
            {
                flag=1;
                break;
            }
        }
        if(flag!=1)
        {
        res*=i;
        str=str+ "*" +i;      
        }
    }
       System.out.println(num +"#= " +str +"=" +res);
    }
    
    public static void main()
    {
        primorial ob=new primorial();  //creating object of primorial class
        ob.getnum();                   //calling method
        ob.findprimorial();            //calling method
    }
   
}

OUTPUT:

Enter a number
13
13#= 2*3*5*7*11*13=30030


/*---------------Program developed by: Nitendra Kumar---------------*/
//For more details visit http://javawithnitendra.blogspot.in


Tuesday, May 21, 2013

Program to print Tribonacci Series using Recursive Technique


/**
 * class tribonacci_ser_recur prints tribonacci series using recursive technique.
 * @author: Nitendra Kumar
 * @version: May 21/2013
 */
import java.util.Scanner;
public class tribonacci_ser_recur
{
   public int trib(int n)
   {
       if(n==1)
         return 0;
         else if(n==2)
         return 1;
         else if(n==3)
         return 1;
       else if(n>3)
         return (trib(n-1)+trib(n-2)+trib(n-3));
         else 
         return -1;
    }
    
    public static void main()
    {
        int res;
        Scanner in=new Scanner(System.in);
        tribonacci_ser_recur ob=new tribonacci_ser_recur();
        System.out.println("Enter the limit");
        int limit=in.nextInt();
        for(int i=1;i<limit;i++)
        {  
        res=ob.trib(i);
          System.out.print(res+" ");
        }
    }
}

Output:


Enter the limit
10
0 1 1 2 4 7 13 24 44


/*---------------Program developed by: Nitendra Kumar---------------*/
//For more details visit http://javawithnitendra.blogspot.in