Showing posts with label Prime Number. Show all posts
Showing posts with label Prime Number. Show all posts

Prime Number

Prime number in Java: Prime number is a number that is greater than 1 and divided by 1 or itself only. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

 import java.util.Scanner;

class prime

{//class prime opens

    public static void main(String[]args)

    {//main method opens

        

        Scanner sc=new Scanner(System.in);

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

       int n=sc.nextInt();//variable n to accept the no

        int c=0;//variable c to count the no of factors

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

        {

           if(n%i==0)

            c=c+1;

        }

        if(c%2==0)

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

        else

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

    }//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 ...