1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| class PrimeNo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
{
System.out.println(num+" is not a Prime Number");
flag = 1;
break;
}
}
if(flag==0)
System.out.println(num+" is a Prime Number");
}
} |
class PrimeNo{
public static void main(String args[]){
int num = Integer.parseInt(args[0]);
int flag=0;
for(int i=2;i<num;i++){
if(num%i==0)
{
System.out.println(num+" is not a Prime Number");
flag = 1;
break;
}
}
if(flag==0)
System.out.println(num+" is a Prime Number");
}
}
import java.io.*;
public class Usersfriendly
{
private static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws IOException
{
int choice;
do
{
System.out.println(“INPUT YOUR NUMBER”);
int m = Integer.parseInt(stdin.readLine());
System.out.println(“1.PRIME NUMBERS”);
System.out.println(“2.REAL NUMBERS”);
System.out.println(“EXIT FROM WIZARD”);
System.out.println(“ENTER THE CHOSEN”);
choice = Integer.parseInt(stdin.readLine());
switch(choice);
{
case1:
int flag=0;
for(i=2;i<m;i++)
{
if(m%i==0)
{
System.out.println(m+"IS NOT PRIME");
flag=1;
break;
}
}
if(flag==0)
System.out.println(m+"IS PRIME!");
case2:
flag=0;
{
if(m%2==0)
{
System.out.println(m+"IS EVEN");
flag=1;
break;
}
}
if(flag==0)
System.out.println(m+"IS ODD")
}
case 3:break;
default: System.out.println("ERRORRRR!");
while (choice!=3);
}
}
}
any mistake in this program ?
pls give suggestions!
Hi,
After switch u had a ‘;’
case should be followed by a blank space
n should be declared
and you used an adiitional } in case 2
while should be written after a {
try it after modifying these.
Hi,
After switch u had a ‘;’
case should be followed by a blank space
n should be declared
and you used an adiitional } in case 2
while should be written after the }
try it after modifying these.
Great helpg buddyzzzzz…… thnx yar…..
hi abitha wat dng
you name s sexy
Simple and Esay
hai thank you program and wat happend if we use any other variable other than flag?
thnx 4 nothin coz dis programm is gvn
“Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0
at primeno.main(primeno.java:7)
“
It can be done in a much much easy way:
import java.io.*;
class PrimeOrNot
{
public static void main() throws IOException
{
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
System.out.println(“Enter the number”);
double n = Double.parseDouble(br.readLine());
System.out.println();
int k, l=0;
for (k=1; k<=n; k++)
{
if(n%k==0)
{
l=l+1;
}
}
if(l==2)
{
System.out.println();
System.out.println(n+" is a prime number");
}
else
{
System.out.println();
System.out.println(n+" is NOT a prime number");
}
}
}
in this prime no. program
no.15,27,21 are showing prime; but they are not;
it is enough to check i<Math.sqrt(n)
this web site is very good …bcoz I do very easily work on it and thanks alot
if i enter number “1” or “0” this code is giving me that it is prime but it is not y??
New way to find prime number,mostly asked aptitudes and interviews http://www.javaengineeringprograms.com/program-for-prime-number-in-java/