Prime number

This is a Java program to check whether a given number is prime or not.

A prime number is a non-negative integer which is divisible only by 1 and itself. For example, Five being divisible by only one and five, is a prime number. Six, on the other hand is divisible by one, two, three and six and hence is not a prime number.

PROGRAM CODE

import java.io.*;
class prime
{
public void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
int a=Integer.parseInt(br.readLine());
int flag=0;  
for(int i=2;i<=a/2;i++)                                              
{
if (a%i==0)
{
flag=1;
break;}}
if(flag==0)
System.out.println("PRIME");
else
System.out.println("NOT PRIME");
}}

Comments

  1. why do such jhanjhat??? I'll give a simpler program for prime number:-


    import java.util.*;
    class p20
    {
    public static void main()
    {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter a number:");
    int k=0,num=sc.nextInt();
    for(int i=1;i<num;i++)
    {
    if(num%2==0)
    k++;
    }
    if(k==1)
    System.out.println("PRIME NUMBER");
    else
    {
    System.out.println("COMPOSITE NUMBER");
    break;
    }
    }
    }

    ReplyDelete
    Replies
    1. ty this was simple and good ty again

      Delete
    2. it is wrong
      the statement "if(num%2==0)"
      should be "if(num%i==0)"

      Delete
    3. "break;"-this statement should not be there
      except that,its all fine

      Delete
    4. the top code is the best efficient ...
      urs is also correct with the above correction but it is time consuming

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. kid ur program is wrong.compile and check it.LoL

    ReplyDelete
  4. A great site for help of the icse.All developers of the site should be honoured.

    ReplyDelete

  5. Divide a given number by its divisors and if find no whole-number factor [apart from 1 and itself] then I will call my number a prime number.The driving force of the operation, or what separates Primes from non-Primes is the division.

    ReplyDelete

Post a Comment

Popular posts from this blog

Automorphic number

Bubble sort