0% found this document useful (0 votes)
14 views

Program n26

Java Program

Uploaded by

debojit052007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Program n26

Java Program

Uploaded by

debojit052007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Program no: 26

import java.util.*;

public class Program_26

int n,c=0,v=0,i;

int[] arr,arr1;

public void accept()

Scanner s = new Scanner(System.in);

System.out.println("Enter a number between 1 and 15: ");

n=s.nextInt();

if (n>=1 && n<=15)

proceed();

else

System.out.println("Invalid Input. Please try again.");

accept();

public void proceed()

if ((n%2)!=0)//checking if number is odd

System.out.println(n + " is not a Goldbach Number");


System.exit(0); //for normal termination of Program

else if(n%2==0) //checking only for even numbers

arr = new int[n]; //defining length of Integer array arr

arr1 = new int[n]; //defining length of Integer array arr1

for (i = 1; i <= n; i++)

int P1 =i; //initializing P1 as index

int P2 =(n-i);

if (isPrime(P1, 2) && isPrime(P2, 2))

arr[c++]=P1; //inserting value of P1 into arr

arr1[v++]=P2; //inserting value of P2 into arr1

print(); //calling void print()

private boolean isPrime(int num, int divisor)

if (num<=1) // Numbers less than or equal to 1 are not prime

return false;

if (divisor>Math.sqrt(num)) // No divisors found, so prime


return true;

if (num%divisor==0) // Found a divisor, not prime

return false;

return isPrime(num, divisor+1); // Check the next divisor

public void print()

if (n<=4)

System.out.println(n + " is not a Goldbach Number");

else

{ //printing Prime Pairs below else-statement

System.out.println(n+" is a Goldbach Number"+"\nAll Prime Pairs are:");

for (i=0;i<c;i++) //printing prime pairs using loop

System.out.println("(" + arr[i] + ", " + arr1[i] + ")");

public void main(){

Program_26 ob = new Program_26();

ob.accept();

}
Output:

You might also like