Tuesday, June 11, 2013

Program to generate Palprime numbers(numbers which are both palindrome and prime)

PROGRAM

Palprime Numbers. Palprime numbers are the palindromic prime numbers i.e.,  the prime numbers that are palindrome also.
First palprime number is a 2-digit (2-d) number and it is 11. There is only one palprime, which is 2-digits long.
To generate a 3-digit(3-d) palprime number, there are many methods. One method is where one just needs to insert a 1-d palprime number in between 11 and then test for its primeness.
This way only two palprimes are generated(by inserting 3 and 5 between 11)
     131 and 151
(insertion of 7 and 11 does not yield a prime number)
You need to write the method GenPalPrime for the following  class that generates palprimes using the method mentioned here.
  public class PalPrime
{
  public static void GenPalPrime(int n)
  {
      /*The passed number n tells the number of digits in the palprime
        to be generated. Acceptable values for n are 2-5*/
   }
The 5-d palprimes may be generated by inserting some 3-d palprimes in 11 e.g., insertion of 131,383,797 yields these palprime numbers: 11311, 13831, 17971.
You may define additional methods, if needed.
Sample Input Output:
Enter the width of palprime numbers (ie no of digits in a palprime no):  2
11
Enter the width of palprime numbers (ie no of digits in a palprime no):  3
131

151
----------------------------------------------------------------------------------------------------------

/**
 * class PalPrime generates palprime numbers.
 * @author: Nitendra Verma 
 * @version: June 11,2013
 */
import java.util.Scanner;
public class PalPrime
{
  public static boolean isprime(int n) //method, checks whether a number is prime or not
  {
      int flag=1;
    for(int i=2;i<n;i++)  
    {
        if(n%i==0)
        {
            flag=0;
            break;
        }
    }
    if(flag==1)
      return true;
    else
      return false;
  }
  
  public static boolean ispal(int n)  //method, checks whether a number is palidrome or not
  {
      int n1,r1,ntemp,rev=0;
      ntemp=n;
      do
      {
          n1=n/10;
          r1=n%10;
          rev=rev*10+r1;
          n=n1;     
        }
        while(n1>0);
        if(rev==ntemp)
           return true;
        else
           return false;
  }

  
  public static void GenPalPrime(int n)  //method to generate numbers which are prime and palindrome both
  {
    int count=0;
    if(n==2)
    {
     System.out.println("11");
     count++;
    }
  else if(n==3)
   {
       int begnum=101;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=3;i<10;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);
               count++;
            }
            }
        }
    }
    
     else if(n==4)
   {
       int begnum=1001;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=10;i<100;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);               
               count++;
               }
            }
        }        
    }
    
    else if(n==5)
   {
       int begnum=10001;
       int lnum=0;
       boolean boolpr=false;
       boolean boolpl=false;
       for(int i=100;i<1000;i++)
       {
           boolpr=isprime(i);
           boolpl=ispal(i);
           if(boolpr==true && boolpl==true)
           {
               lnum=begnum+i*10;
               if(isprime(lnum)==true && ispal(lnum)==true)
               {
               System.out.println(lnum);
               count++;
            }
            }
        }       
    }
    else
     System.out.println("Value of n should be within range 2-5");
    if(n>=2 && n<=5 && count==0)
     System.out.println("No palprime number of this width");
}

 public static void main()
 {
     PalPrime ob=new PalPrime();  //creating object of class PalPrime
     Scanner in=new Scanner(System.in);  //creating object of class Scanner
     System.out.println("Enter the width of palprime numbers(i.e., no of digits in a palprime no):");
     int num=in.nextInt();
     GenPalPrime(num);  //method calling
 }
}

OUTPUT
Enter the width of palprime numbers(i.e., no of digits in a palprime no):
3
131
151


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

Program for Christian Goldback

PROGRAM
Christian Goldback has conjectured for N<1014 that every even N>2, can be represented as sum of two primes. For example, 18=5+13.

WAP that reads N which should be an even number>2 and represents it as sum of two primes.
-----------------------------------------------------------------------------------------------------

/**
 * class ChristianGoldback find two prime numbers whose sum is equal to a given number.
 * @author: Nitendra Verma 
 * @version: June 10,2013
 */
import java.util.Scanner;
public class ChristianGoldback
{
 int n,count=0;  //instance variables
    public void getnum(int nn) //method to assign parameter to number 
    {
        n=nn;
    }
    
    public void check()  //method to find prime numbers whose sum is equal to given number
    {
        int g1[]=new int[n];
        int flag=0;       
        for(int i=3;i<n;i++)  //outer loop
        {
            flag=1;
        for(int j=2;j<i;j++)  //inner loop
        {
           if(i%j==0)
           {
               flag=0;
               break;
            }           
        }
        if(flag==1)
        {
            g1[count]=i;
            count++;
        }
        
    }
        System.out.println("Number can be represented as sum of two primes:");
        for(int i=0;i<count/2;i++)
        {
            for(int j=0;j<count;j++)
            {
            if((g1[i]+g1[j])==n)
            {
                System.out.println(n +"=" +g1[i]+"+" +g1[j]);
            }
            }
        }
    }
    
    public static void main()
    {
        ChristianGoldback ob=new ChristianGoldback(); //creating object of class ChristianGoldback
        Scanner in=new Scanner(System.in);   ////creating object of class Scanner
        System.out.println("Enetr a num");
        int num=in.nextInt();   
        ob.getnum(num);     //calling method
        ob.check();         //calling method
    }
}

OUTPUT
Enetr a num
18
Number can be represented as sum of two primes:
18=5+13
18=7+11


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




Program to find the sum of the series x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6

PROGRAM
WAP to find the sum of the series:
                x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6
-------------------------------------------------------------------------------------------------------------------------------
/**
 * class sumseries2 finds the sum of the series.
 * x-x2/!2+x3/!3-x4/!4+x5/!5-x6/!6
 * @author: Nitendra Verma
 * @version: June 10, 2013
 */
import java.util.Scanner;
public class sumseries2
{
  int num,limit;    //instance variables to store number and limit 
  public sumseries2()  //default constructor to inditilize variables to 0
  {
      num=0;
      limit=0;
    }    
    public void getnum(int n,int l)  //method to assign parameters to varaibles
    {
        num=n;
        limit=l;
    }
    
    public int fact(int n)  //method to find factorial value
    {
        int f=1;
        for(int i=1;i<=n;i++)
        {
            f*=i;
        }     
        return f;           //returning factorial value
    }
    
    public void findsumseries()  //method to find sum of the series 
    {
        float sum=0;
        for(int i=0;i<limit;i++)
        {
            sum+=Math.pow(-1,i)*(Math.pow(num,i+1)/fact(i+1));
        }
        System.out.println("Sum of the series:"+sum);
    }
    
    public static void main()
    {
      sumseries2 ob=new sumseries2();  //creating object of class sumseries2
      Scanner in=new Scanner(System.in); //creating object of class Scanner
      System.out.println("Enetr the number");
      int n=in.nextInt();
      System.out.println("Enetr the limit");
      int l=in.nextInt();
      ob.getnum(n,l);        //calling method
      ob.findsumseries();    //calling method
    }
}

OUTPUT
Enetr the number
2
Enetr the limit
3
Sum of the series:1.3333334



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

Friday, June 7, 2013

Program to print sum of the series S=1+x+x2+x3+…………+xn

PROGRAM
WAP to find  sum of the series S=1+x+x2+x3+…………+xn


/**
 * class sumseries finds sum of the series.
 * This program dosn't use built-in method pow().
 * @author: Nitendra Verma 
 * @version: June 8,2013
 */
import java.util.Scanner;
public class sumseries
{
    int num,limit;     //instance variables
    public sumseries() //default constructor, initializes both variables to 0 
    {
        num=0;
        limit=0;
    } 
    public void getlimit(int n,int l) //method, assigns parameter values to variables 
    {
        num=n;
        limit=l;
    }
    
    public int findpow(int x,int p)  //method, returns power value of number
    {
    int value=1;
        for(int i=0;i<p;i++)
        {
            value*=x;
        }
        return value;   //returning value
    }
    
    public void findsumseries()  //method, finds sum of the series
    {
        int sum=0;
        for(int i=0;i<limit;i++)
        {
            sum+=findpow(num,i);
        }
        System.out.println("Sum of the series= "+sum);
    }
    
    public static void main()
    {
        sumseries ob=new sumseries();   //creating object of class sumseries
        Scanner in=new Scanner(System.in);  //creating object of class Scanner
        System.out.println("Enetr the number");
        int num=in.nextInt();               //inputting value
        System.out.println("Enetr the limit");
        int limit=in.nextInt();             //inputting value
        ob.getlimit(num,limit);             //calling method
        ob.findsumseries();                 //calling method
    }
}

OUTPUT:
Enetr the number
3
Enetr the limit
3
Sum of the series= 13

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

Program to check whether a number is Magic Number or not

PROGRAM
A Magic Number is a number in which the eventual sum of digits of the number is equal to 1.
For example,       172=1+7+2=10
                                 10=1+0=1
then 172 is a Magic Number.
 Design a class Magic to check if a given number is a magic number. Some of the members of the class are given below:
Class Name                        :               Magic  
Data members/instance variables  :
n                                             : stores the number
Member functions :
Magic()                                 : constructor to assign 0 to n
void getnum(int nn)       : to assign the parameter value to the number, n=nn
int Sum_of_digits(int)    : returns the sum of the digits of a number
void ismagic()                    : checks if the given number is a magic number by calling the function   

                                                  Sum_of_digits(int) and display appropriate message.
Specify the class Magic giving details of the constructor, void getnum(int), int Sum_of_digits(int) and void ismagic(). You do not need to write the main function.

/**
 * class magicnum checks whether a number is Magic Number or not. 
 * @author: Nitendra Verma
 * @version: June 8,2013
 */
import java.util.Scanner;
public class Magic
{
    int n,ntemp;  //instace variable to store the number
    public Magic()  //default constructor, assigns 0 to n
    {
        n=0;
    }
    
    public void getnum(int nn)  //metod, assigns the parameter value to the number
    {
        n=nn;
    }
    
    public int Sum_of_digits()  //method, returns the sum of digits of number 
    {
        int n1,r1,sum=0;
        ntemp=n;
        do      //outer loop
        {
        sum=0;  
        do     //inner loop
       {
           n1=n/10;
           r1=n%10;
           sum+=r1;
           n=n1;
        }
        while(n1!=0);
        n=sum;       
    }
    while(sum>=10);
    return sum;    //returning sum
    }
    
    public void ismagic()    //method, checks if the number is Magic Number 
    {
        int sum=Sum_of_digits();
        if(sum==1)
          System.out.println(ntemp+" is a Magic Number");
        else
          System.out.println(ntemp+" is not a Magic Number");
    }
    
    public static void main()
    {
        Magic ob=new Magic();   //creating object of Magic class
        Scanner in=new Scanner(System.in);  //creating object of Scanner class
        System.out.println("Enter a number");
        int num=in.nextInt();   //inputting number
        ob.getnum(num);         //calling method
        ob.ismagic();           //calling method
    }
}

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

Enter a number
172
172 is a Magic Number

Wednesday, June 5, 2013

Program to print Tribonacci series


/**
 * class tribonacci_series prints the Tribonacci Series
 * Tribonacci Series: 0,1,1,2,4,7,13,24,44,81
 * @author: NItendra Kumar 
 * @version :May 21/2013
 */
import java.util.Scanner;
public class tribonacci_series
{
 public static void main()
 {
     Scanner in=new Scanner(System.in);
     System.out.println("Enetr the limit");
     int limit=in.nextInt();
   int num1,num2,num3,sum;
   num1=0;
   num2=1;
   num3=1;
   System.out.print(num1 +"," +num2+","+num3+",");
   for(int i=0;i<limit;i++)
   {
       sum=num1+num2+num3;
       num1=num2;
       num2=num3;
       num3=sum;
       System.out.print(sum+",");
    }
}
}

Output:

Enter the limit
10
0,1,1,2,4,7,13,24,44,81,149,274,504,


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

Tuesday, June 4, 2013

Program to check whether a number is Deficient number, Perfect number or Abundant number

PROGRAM
Suppose you take a positive integer n and add its (positive) divisors. E.g.  if n=18, then sum=1+2+3+6+9+18=39. In general, when we do this with n, one of the following three happens:
If the sum is
n is said to be
e.g.
Less than 2n
Deficient number
1,2,3,4,5,8,9
Equal to 2n
Perfect number
6,28,496
Greater than 2n
Abundant number
12,18,20,24,30

WAP that inputs a number and determines whether it is a Deficient number or a Perfect number or a Abundant number.
SAMPLE DATA

Enter a no: 28                                    It is a Perfect number
Enter a no: 30                                    It is an Abundant number
--------------------------------------------------------------------------------------------------------

/**
 * class checknumber checks whether a number is Deficient number,
 * Perfect number or Abundant number.
 * @author: Nitendra Kumar 
 * @version: June 5,2013
 */
import java.util.Scanner;
public class checknumber
{
    public int num=0;
    public void getnum()  //method to accept a number from user
    {
        Scanner in=new Scanner(System.in);  //creting an object of Scanner class
        System.out.println("Enter a number");
        num=in.nextInt();
    }
    public void fnchecknumber()  //method to check type of number
    {
        int sum=0,dnum=0;
        for(int i=1;i<=num;i++)  
        {
            if(num%i==0)
              sum+=i;
        }
        dnum=2*num;
        if(sum<dnum)
          System.out.println("It is a Deficient number");
        else if(sum==dnum)  
          System.out.println("It is a Perfect number");
        else if(sum>dnum)
          System.out.println("It is an Abundant number");
    }
    
    public static void main()   //Entry point of program
    {
        checknumber ob=new checknumber();  //creating object of class checknumber
        ob.getnum();                       //calling method
        ob.fnchecknumber();          //calling method
    }
}

OUTPUT:
Enter a number
30
It is an Abundant number


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


                      For eBook on Language C click here..

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