Thursday, 8 February 2018

Computer Science (Practical) Question - ISC Board 2018

import java.util.*;
import java.io.*;

Class Goldbach
{

 int i, num, n, m, a, b, k, x, j, number;

        int goldbach(int i, int a, int b)
        {

                for (x = 2; x <= (i / 2); ++x)
                {
                        if(prime(x) && prime(i - x))
                        {
                                a = x;
                                b = i - x;
                                return 1;
                                break;
                        }


                }
        }


int prime(int j)
{
        int result = 1, k;

        if(j == 1 || j == 2)
        {
                result = 0;
        }
        else
        {
                for(k=2;k<=j/2;k++)
                {
                        if(j%k == 0)
                        {
                                result = 1;
                                break;
                        }
                }
        }

        if(result == 0)
                return 1;
        else
                return 0;
}


public static void main (String agrs[])
{
 Goldbach ob = new Goldbach();
 Scanner obj = new Scanner(System.in);

        System.out.println("Please enter the value of n: ");
        n=obj.nextInt();
        System.out.println("Please enter the value of m: ");
        m=obj.nextInt();

        if (n < 4)
        {
                num = 4;
        }
        else
        {
                if (n%2 != 0)
                {
                        num = n + 1;
                }
        }

        for (i = num; i <= m; i += 2)
        {
                if(ob.goldbach(i, a, b))
                {
                        System.out.println(i+""+a+""+b);
                }
                else
                {
                        System.out.println("Goldbach's Conjecture fails for this number: \n", i);
                }
        }
}
}

No comments:

Post a Comment