Java program to print Twin Prime Numbers

Java program to print Twin Prime Numbers

Java program to print Twin Prime Numbers

Write a program in java to enter two numbers and check if they are twin prime numbers or not.

Twin Primes are the prime numbers with a difference of 2, e.g., (3, 5), (5, 7), (11, 13), (17, 19), (29, 31) ... etc. 

In this program isPrime(int n) will return true if the number is prime and false if it is not prime. This method is called in the main() method which checks the numbers are prime or not and also the difference between two prime numbers is 2. And if it is then it called Twin prime number.


Solution
import java.io.*;
// Program to check twin prime
public class TwinPrimeNumber
{
 
        public static boolean isPrime(int n)
        {
            // boolean value will return
            boolean f = true;

            for (int i = 2; i <= n / 2; i++) {
                if (n % i == 0) {
                    f = false;
                    break;
                }
            }

            return f;
        }

        // main method begins
        public static void main(String args[]) throws IOException
        {

            int number1, number2;
            // InputStreamReader object
            InputStreamReader in = new InputStreamReader(System.in);
            // BufferedReader object
            BufferedReader br = new BufferedReader(in);    
            System.out.print("Enter first number: ");
            // First number
            number1 = Integer.parseInt(br.readLine());
            System.out.print("Enter second number: ");
            // Second number
            number2 = Integer.parseInt(br.readLine());

            // Checking both the number is prime and the difference between two is 2
            if (isPrime(number1) == true && isPrime(number2) == true && Math.abs(number2 - number1) == 2) {
                System.out.println("Twin prime number");
            } else {
                System.out.println("Not twin prime numbers");
            }

        } // end method main
    } // end class



Output
Enter first number: 11
Enter second number: 13
Twin prime number

Enter first number: 9
Enter second number: 11
Not twin prime numbers

In the above program, a method public static boolean isPrime(int n) is used to check prime numbers. This method return a boolean value. If the number is prime then it will send true otherwise false. In main() method isPrime called two times to check true or false. 

//Twin prime number without method

import java.util.*;
public class TwinPrime
{
    public static void main(String args[])
    {
        int n1, n2, i, f1,f2;
        f1=f2=1;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter First number:");
        n1=sc.nextInt();
        System.out.println("Enter Second number:");
        n2=sc.nextInt();
        for(i=2; i<n1; i++)
        {
            if(n1%i==0)
            {
                f1=0;
                break;
            }
        }
        for(i=2; i<n2; i++)
        {
            if(n2%i==0)
            {
                f2=0;
                break;
            }
        }
        if(f1==1 && f2==1 && Math.abs(n1-n2)==2)
        {
            System.out.println("Twin Prime Number");
        }
        else
        {
            System.out.println("Not a Twin Prime Number");
        }
    }
}

More Program







Java program for Brun’s Constant and Twin Prime number

Java program for Brun’s Constant and Twin Prime number

Java program for Brun’s Constant and Twin Prime number

Twin Primes are the prime numbers with a difference of 2, e.g., (3, 5), (5, 7), (11, 13), (17, 19) ... etc. The sum of reciprocals of the twin primes up to a limit, converges to sum known as Brun’s Constant. Example :
Say for Inputted number 13 and its twin primes are (3, 5), (5, 7), (11, 13) and its sum of reciprocals is
 (1/3 + 1/5) + (1/5 + 1/7) + (1/11 + 1/13) =  1.044022644022644
Write a program to create Primes which include methods:
Booleans isPrime(int n) Checks whether n is prime or not
Void twinPrimesBrunConstant – Which checks twinPrime and also calculate brun’s constant.


Solution
import java.util.Scanner;
/**
 * class BrunsConstant
 * @author (InspireSkills)
 * Input 2 nos. and check whether they make up Brun’s Constant or not, Brun’s Constant is sum of
 * reciprocal of Twin Prime Nos. e.g (1/3) + (1/5) is a Brun’s Constant
 */
public class BrunsConstant
{
    static double sum=0.0;
    static int i, f;
    // Check the number prime or not
    public static boolean isPrime(int n)
    {
        f=1;
        for (i = 2 ; i < n ; i++)
        {
            if(n % i == 0)
            {
                f=0;
                break;
            }
        }
        if(f == 1)
            return true;
        else
            return false;

    }


    // Here checked the two number twin prime or not
    // and also calculate brun's constant.
    public static void twinPrimesBrunConstant(int number )
    {
        System.out.println("Twin prime");
        System.out.println("==========");
        for (int i = 2; i < number ; i++)
        {
            int num1 = i;
            int num2 = i + 2;
            if (isPrime(num1) && isPrime(num2))
            {
                System.out.println(num1 + " " + num2);
                sum = sum + (double)1/num1 + (double)1/num2;
            }
        }
    }

    public static void main(String[] args)
    {
        int no;
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number: ");
        no = scanner.nextInt();
        twinPrimesBrunConstant(no);
        System.out.println("Brun's Constant:" + sum);
    }
}

Advanced Java : Learn Java Advanced Features


Output
Enter a number: 13
Twin prime
==========
3 5
5 7
11 13
Brun's Constant:1.044022644022644
Enter a number: 50
Twin prime
==========
3 5
5 7
11 13
17 19
29 31
41 43
Brun's Constant:1.2698646333745234


More Program

Java program to check palindrome



Java program to check palindrome

Java program to check palindrome

Java program to check palindrome

Palindrome Number


A Palindrome number or word which will read the same from either side, backward of forward.
For example 202 or madam read same from both forward and backward.

Java Program for Palindrome Number

import java.io.*;
public class PalindromeNumber
{
    public static void main(String args[])throws IOException
    {
        int n, rev, a ,m;
        rev=0;         
        InputStreamReader ab = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(ab);
        System.out.print("Enter number to Palindrome:");
        n = Integer.parseInt(br.readLine());
        m = n;
        // Reversing the number
        while(n != 0)
        {
            a = n % 10;
            rev = rev * 10 + a;
            n = n / 10;
        }
        System.out.println("Reverse Number:" + rev);
        // Checking Palindrome or not
        if(m == rev)
        {
            System.out.println("Palindrome");
        }
        else
        {
            System.out.println("Not Palindrome");
        }
    }
}

Output
Enter number to Palindrome:202
Reverse Number:202
Palindrome

Enter number to Palindrome:221
Reverse Number:122
Not Palindrome

Java Program for Palindrome Word or String

For String Plaindrome
import java.util.Scanner;
/**
 * class Palindrome Word here.
 * Khurshid Md Anwar (InspireSkills)
 */
public class PalindromeWord
{
   public static void main(String args[])
   {
       Scanner sc = new Scanner(System.in);
       String st; // Variable for Word taken
       System.out.println("Enter a string or word ");
       st=sc.nextLine();
       int i,l;
       char ch;
       String revWord="";  // for reverse Word
       l = st.length();    // Finding the length of word
       for(i=0;i<l;i++)
       {
           ch = st.charAt(i);
           revWord = ch + revWord;
        }
         // Checking Plaindrome or not
        if(st.equals(revWord))
        {
            System.out.println("Palindrome Word");
        }
        else
        {
            System.out.println("Not a Palindrome Word");
        }       
    }
}

Output
Enter a string or word
madam
Palindrome Word
Enter a string or word
hello


Not a Palindrome Word

Visit for More Program

java program for isbn number




Addition of two matrices in Java

Addition of two matrices in Java

Addition of two matrices in Java


Java matrix addition


In this program, we are performing the addition of two matrices. Here we define two matrix one is first[][]
and another is second[][] and another one defines for summation of two entered matrices. First, we take row and column and then enter the values in two matrices. After then we sum the two matrices.



import java.util.Scanner;
/**
 * Java program for
 * Addition Of Two Matrices
  */
public class AdditionOfTwoMatrices
{
    public static void main(String args[])
    {
        int m, n, i, j;        // Local Variables
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the number of rows and columns of matrix");
        m = sc.nextInt();
        n  = sc.nextInt();
        // Array definition 
        int first[][] = new int[m][n];       // First Array
        int second[][] = new int[m][n]; // Second Array
        int add[][] = new int[m][n];       // sum of two matrix array

        System.out.println("Enter the elements of first matrix");

        for (  i = 0 ; i < m ; i++ )
        {
            for ( j = 0 ; j < n ; j++ )
            {
                first[i][j] = sc.nextInt();
            }
        }

        System.out.println("Enter the elements of second matrix");

        for (  i = 0 ; i < m ; i++ )
        {
            for ( j = 0 ; j < n ; j++ )
            {
                second[i][j] = sc.nextInt();
            }
        }

        // Display of First Matrix
        System.out.println("Output of First Matrix");
        for (  i = 0 ; i < m ; i++ )
        {
            for ( j = 0 ; j < n ; j++ )
            {
                System.out.print(first[i][j] + " ");
            }
            System.out.println();
        }

        // Display of Second Matrix
        System.out.println("Output of Second Matrix");
        for (  i = 0 ; i < m ; i++ )
        {
            for ( j = 0 ; j < n ; j++ )
            {
                System.out.print(second[i][j] + " ");
            }
            System.out.println();
        }

        // Addition of matrix
        for (  i = 0 ; i < m ; i++ )
        {
            for ( j = 0 ; j < n ; j++ )
            {
                add[i][j] = first[i][j] + second[i][j];
            }
        }
       // Display of Matrix addition
        System.out.println("Addition of Two Matrix");

        for (  i = 0 ; i < m ; i++ )
        {
            for ( j = 0 ; j < n ; j++ )
            {
                System.out.print(add[i][j]+"\t");
            }
            System.out.println();
        }
    }
}


Output

Enter the number of rows and columns of matrix
2
2
Enter the elements of first matrix
7
8
9
4
Enter the elements of second matrix
5
6
1
2
Output of First Matrix
7 8 
9 4 
Output of Second Matrix
5 6 
1 2 
Addition of Two Matrix
12 14
10 6

Visit for more programs