Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
Java find twin primes
Sponsered Links
Categories
Sponsered Links

Java find twin primes

A twin prime is a prime number that differs from another prime number by two.For example, (3,5), (5,7), (11,3) etc. Here we are going to find the twin primes between 1 and 100.

class TwinPrimes {


    public static void main(String args[]) {


        String primeNo = "";


        int j = 0;


        int LastPrime = 1;


        System.out.println("Twin Primes are:");


        for (int i = 1; i < 100; i++) {


            for (j = 2; j < i; j++) {


                if (i % j == 0) {


                    break;


                }


            }


            if (i == j) {


                primeNo += i + " ";


                if ((i - LastPrime) == 2) {


                    System.out.println("(" + (i - 2) + "," + i + ")");


                }


                LastPrime = i;


            }


        }


        System.out.println("Prime Numbers are: " + primeNo);


    }


}

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved