Hi. I am learning JAVA programming language. I want to write code for to check the weather given number is prime or not. I tried lots of method bit it will give error message. Please give me suitable code for that. Thank you.
Hi. I am learning JAVA programming language. I want to write code for to check the weather given number is prime or not. I tried lots of method bit it will give error message. Please give me suitable code for that. Thank you.
Try this:
**)Code:class Prime_number { public static void main(String[] args) { int num = 21; int i; for (i=2; i < num ;i++ ){ int n = num%i; if (n==0){ System.out.println("not Prime!"); break; } } if(i == num){ System.out.println("Prime number!"); } } }
I have tried the given code but it didn’t solve my problem. Please try to provide some another solution.
So just have a look at the coding which can make you to understand more properly:
Code:static public IsPrime(int number) { if (number == 1 || number == 2) return 1; for (int i=2; i<(int)(number/2); i{ if ( (number/i)==(int)(number/i) ) return 0; } return 1; }
Bookmarks