Prime number Program in Java
Hello friends; Hitesh is here, Today we will learn about how to create Prime number Program in Java. For this program you need to follow normal concept of C language which is already you learn only need to change syntax of C language into Java Syntax. Prime number is only divisible by 1 and itself number.
class PrimeExample
{
public static void main(String args[])
{
int i,m=0,flag=0;
int n=21; // Check 21 is prime number or not
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
System.out.println("Number is not prime");
flag=1;
break;
}
}
if(flag==0)
System.out.println("Number is prime");
}
}
Output:
Number is not prime number
I hope this code is helpful for you, but this is normal code for more detail you need to visit our website www.tutorial4us.com.
No comments:
Post a Comment