Hello Everyone, I am looking to some prime number program with no provision for input and also want to know of prime number algorithm, how to check this. I have added one program. Can anyone check this and tell me is it no provision input program?
publicclassPrimeCheck{
publicstaticvoidmain(String args[]){
int i,m=0,flag=0;
int n=3;
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not a prime number.");
}
else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not a prime number.");
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is a prime number."); }
}
}
}
The above given code is not correct, If you want to check that your program is working fine or not then you can run your code here: https://www.onlinegdb.com/online_java_compiler This is the online compiler and you can find that your code is running fine or not, If your program is not functional than it mean that has some problem while writing.
Here I am giving you an example of complete code:
public class PrimeCheck{
public static void main(String args[]){
int i,m=0,flag=0;
int n=3;
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not a prime number.");
}
else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not a prime number.");
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is a prime number."); }
}
}
}
To know more about the explanation and behind the process of the java code visit this blog and learn more.
Prime Number Program in Java
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.