Prime number program in Java

0

 Finding the prime number is one of the most basic problems of mathematics . In this post we are going to see how can we implement this code in java .

First of all what is Prime Number - A number is called prime number when it is divisible by two numbers: either 1 or itself. So, if there is any number which is is divisible by any other number, that number is not called a prime number.


Input : number  = 10 ; 

Output : No

Explanation : 10 is divisible by 1,2,5,10 . So it is not a prime number.



Input : number  = 7 ; 

Output : Yes

Explanation : 7 is divisible by only  1,7. No other number can divide it . So it is a prime number.



public class HindiCodingCommunity

{

public static void main(String [] args)

{
int num = 11;
boolean flag = false;

for (int i = 2; i <= num / 2; ++i)
        {
if (num % i == 0)
            {
    flag = true;
    break;
}
}

if (!flag)
System.out.println("prime number");
else
System.out.println("Not a prime number");
}
}


Output : Not a prime number



Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !