Tuesday, June 11, 2013

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