Showing posts with label Twin Primes Numbers. Show all posts
Showing posts with label Twin Primes Numbers. Show all posts

Twin Primes Numbers

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.

 import java.util.*;

class twinprime 

{//class twinprime opens

    int a=0,b=0;

    void input()//method to input the nos

    {

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        a=sc.nextInt();

        System.out.println("Enter another no");

        b=sc.nextInt();

    }

    boolean isprime(int n)//method to check whether a no is prime or not

    {

        int c=0;

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

        {

            if(n%i==0)

            c=c+1;

        }

        if(c==2)

        return true;

        else

        return false;

    }

    void display()//method to print the result

    {

        if(((isprime(a)==true)&&(isprime(b)==true))&&((a-b==2)||(b-a==2)))

        {

            System.out.println("It is a twinprime no");

        }

        else

        System.out.println("It is not a twinprime no");

    }

    public static void main(String[]args)

    {//main method opens

        twinprime obj=new twinprime();

        obj.input();

        obj.display();

    }//main method closes

}//class closes


            

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...