How to find Prime Number || Basic Programming Questions Series

preview_player
Показать описание
In this video, we will learn how to write a program to check the given number is prime or not.

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

~~~Subscribe to this channel, and press bell icon to get some interesting videos on Selenium and Automation:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:

WebServices API Automation Tutorials:

Follow me on my Facebook Page:

Let's join our Automation community for some amazing knowledge sharing and group discussion on Telegram:

Paid courses (Recorded) videos:
📗 Get My Paid Courses at
Paid courses (Recorded) videos:
-------------------------------

✔️SOCIAL NETWORKS
--------------------------------

Support My Channel✔️Or Buy Me A Coffee
--------------------------------
✔️Thanks for watching!
देखने के लिए धन्यवाद
Благодаря за гледането
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
Рекомендации по теме
Комментарии
Автор

perfect coding with all negative test cases

manishpatidar
Автор

sir you are such a genius...your explanation is too good very easy to understand. Thank u so much sir.
sir how to develop our coding or logic ??? please suggest sir its difficult to write logic. How can I improve it???

kshitijagaikwad
Автор

I tried your logic in isPrimeNumber without the for loop, I just put (if(num%2==0). i dont think I need the for loop, please clarify

sarakhalifa
Автор

Thank u so much sir for providing such good collection of videos..

dimplekumari
Автор

@Naveen AutomationLabs, again huge thanks ! Iknow u didnt use it here but still : if in isPrime() i say i<num/2 in the loop and i pass any number greater than 4 to thegetPrimeNumbers() - 4 gets printed ! And its not prime . Only if i say i<=num/2 in the isPrime() for loop then it works correctly, why ? Im breaking my brain . Im very close to understanding but cant nail it .
p.s Watching all your videos, youre the best !

igoril
Автор

Can you please make more videos on programming which will be asked for testers in interview

jayalakshmig
Автор

You are the best Naveen, in all your videos that is! Popular in NYC!! Whats the best way to contact you for some offers?

samuam
Автор

One question Naveen, i saw some programs on internet for checking prime numbers and there logic is including i<num/2, so why num/2, has been written in for loop, will u pls explain.

Tidda
Автор

/**
* This program has two methods, one of which tells if a number is prime or not
* and the second method returns all the list of prime numbers less than the number you've enetered
*/
package introduction;

/**
* @author saksham
*
*/
public class PrimeNumbers {

/**
* @param args
*/
public static void main(String[] args) {

int i = 23;
if (isPrime(i) == true) {
System.out.print(
"Output 1 : The number you've entered is also prime and all the prime numbers less than and equal to the given number are -> \n");
listPrimenumbers(i);
System.out.println("\n");

} else if (isPrime(i) == false) {
System.out.print("Output 1 : Although the number you've entered (" + i + ") is not a prime number, "
+ "all the prime numbers below that number is/are -> \n");
listPrimenumbers(i);
System.out.println("\n");

}

int j = 24;
if (isPrime(j) == true) {
System.out.print(
"Output 2 : The number you've entered is also prime and all the prime numbers less than and equal to the given number are -> \n");
listPrimenumbers(j);
System.out.println("\n");

} else if (isPrime(j) == false) {
System.out.print("Output 2 : Although the number you've entered (" + j + ") is not a prime number, "
+ "all the prime numbers below that number is/are -> \n");
listPrimenumbers(j);
System.out.println("\n");

}

}

public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}

for (int j = 2; j < num; j++) {
if (num % j == 0) {
return false;
}

}
return true;
}

public static void listPrimenumbers(int num) {

for (int i = 1; i <= num; i++) {
if (isPrime(i) == true) {
System.out.print(i + ", ");

}
}
}

}



Output is :
Output 1 : The number you've entered is also prime and all the prime numbers less than and equal to the given number are ->
2, 3, 5, 7, 11, 13, 17, 19, 23,

Output 2 : Although the number you've entered (24) is not a prime number, all the prime numbers below that number is/are ->
2, 3, 5, 7, 11, 13, 17, 19, 23,



Thanks @Naveen

myamazingvideos
Автор

Well explained ! Just one clarification: If we run the loop till num/2 that will also work ? bcz if any number is not divisible by half of its values it won't be divisible anyways ?

diptmangupta
Автор

Y did u change it from num to i in second static method.. If it is i how it will be calling the first static method. Please clarify

ashwinikunapareddy
Автор

I was getting an error while calling a non-static method in a static method. It seems Java doesn't support this.

shobhitkrishan
Автор

how to print prime numbers by using scanner class?

sunilgowda
Автор

class HelloWorld {
public static boolean isPrime(int num)
{
if(num<=1)
{
return false;
}
else
{
return true;
}
}
public static void main(String[] args) {
System.out.println( "3 is a Prime number : " +isPrime(3));
System.out.println( "0 is a Prime number : " +isPrime(0));
}
}
We could have done like this way?

rqbyhco
Автор

Why do u declare the method as static ?

nabilelassaad
Автор

Wrong
If we give num =15
15 % 2 == 0
1 == 0
So the condition is false
then it returns true
Can you please explain me