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");
}}
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");
}}
why do such jhanjhat??? I'll give a simpler program for prime number:-
ReplyDeleteimport 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;
}
}
}
ty this was simple and good ty again
Deleteit is wrong
Deletethe statement "if(num%2==0)"
should be "if(num%i==0)"
"break;"-this statement should not be there
Deleteexcept that,its all fine
the top code is the best efficient ...
Deleteurs is also correct with the above correction but it is time consuming
This comment has been removed by the author.
ReplyDeletekid ur program is wrong.compile and check it.LoL
ReplyDeleteviladskumar sommthinf
DeleteA great site for help of the icse.All developers of the site should be honoured.
ReplyDelete
ReplyDeleteDivide 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.