Annoucement: New Java and Selenium batch is starting from October 28th Nov, Please enroll ASAP!

How to find Prime Number

How to find Prime Number in Java


Learn:
1. check the given number is prime or not.
2. print all the prime numbers upto a given a number
3. write different test cases




Code:

package Questions;

public class PrimeNumber {
//2 is the lowest prime number
//3
//num = 20
public static boolean isPrimeNumber(int num){
//edge/corner cases:
if(num<=1){
return false;
}
for(int i=2; i<num; i++){
if(num % i == 0){
return false;
}
}
return true;
}
public static void getPrimeNumbers(int num){
for(int i=2; i<=num; i++){
if(isPrimeNumber(i))
System.out.print(i + " ");
}
}
public static void main(String[] args) {
System.out.println("2 is prime number: "+ isPrimeNumber(2));
System.out.println("3 is prime number: "+ isPrimeNumber(2));
System.out.println("10 is prime number: "+ isPrimeNumber(10));
System.out.println("17 is prime number: "+ isPrimeNumber(17));

System.out.println("0 is prime number: "+ isPrimeNumber(0));
System.out.println("1 is prime number: "+ isPrimeNumber(0));

System.out.println("-3 is prime number: "+ isPrimeNumber(-3));
getPrimeNumbers(7);
getPrimeNumbers(13);
getPrimeNumbers(20);



}



}










6 comments:

  1. Magnificent beat ! I wish to apprentice while you amend your web site, how could i subscribe for a blog web site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear concept inbound marketing

    ReplyDelete
  2. This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted.By Learn Digital Marketing Course Training in Chennai it will help to get Digital Marketing Course Training in Chennai.Learn Best Digital Marketing Course Training in Chennai's professional institute to get reputed job.

    ReplyDelete

Featured Post

How to control Chromedriver using curl

How to control Chromedriver using curl Here is how to use Chromedriver without libraries like  selenium-webdriver . This can be useful ...