5.17 How to check a given number is prime or not in Java Tutorial | Lecture

  Рет қаралды 292,355

Telusko

Telusko

9 жыл бұрын

Join the live batch : www.telusko.com/online.htm
40% off on selected courses only for Subscribers, to know more send an email on teluskotraining@gmail.com
Recommend Books :
1. Head First Java : amzn.to/2owFrf0
2. Java Complete Reference : amzn.to/2osY04k
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this video we will see if a number is prime or no. We will take input from user , and check if the number entered by user is prime or not. If we try to divide that number between 1 to that number it will not get divided by any number.
Editing Monitors :
amzn.to/2RfKWgL
amzn.to/2Q665JW
amzn.to/2OUP21a.
Editing Laptop :
ASUS ROG Strix - (new version) amzn.to/2RhumwO
Camera : amzn.to/2OR56AV
lens : amzn.to/2JihtQo
Mics
amzn.to/2RlIe9F
amzn.to/2yDkx5F
Check out our website: www.telusko.com
Follow Telusko on Twitter: / navinreddy20
Follow on Facebook:
Telusko : / teluskolearnings
Navin Reddy : / navintelusko
Follow Navin Reddy on Instagram: / navinreddy20
Subscribe to our other channel:
Navin Reddy : / @navinreddy
Telusko Hindi :
/ @teluskohindi
Subscribe to the channel and learn Programming in easy way.
Java Tutorial for Beginners : goo.gl/p10QfB
Scala Tutorials for Java Developers : goo.gl/8H1aE5
C Tutorial Playlist : goo.gl/8v92pu
Android Tutorial for Beginners Playlist : goo.gl/MzlIUJ
XML Tutorial : goo.gl/Eo79do
Design Patterns in Java : goo.gl/Kd2MWE
Socket Programming in Java : goo.gl/jlMEbg
Spring MVC Tutorial : goo.gl/9ubbG2
OpenShift Tutorial for Beginners : goo.gl/s58BQH
Spring Framework with Maven : goo.gl/MaEluO
Sql Tutorial for Beginners : goo.gl/x3PrTg
String Handling in Java : goo.gl/zUdPwa
Array in Java : goo.gl/uXTaUy
Java Servlet : goo.gl/R5nHp8
Exception Handling in Java : goo.gl/N4NbAW

Пікірлер: 96
@jesuslovesyou2270
@jesuslovesyou2270 8 жыл бұрын
this is the most helpful vid I found to solve this problem. thank u so much sir
@santoshraj219
@santoshraj219 3 жыл бұрын
It has a bug, if someone enter 2 as value it will return as not prime
@SmartProgramming
@SmartProgramming 6 жыл бұрын
nice tutorial sir, some of them are so helpful, thanks and keep it up 👍👍
@vampiriclion1176
@vampiriclion1176 4 жыл бұрын
Smart Programming, so you learn from other videos? I have seen some of your videos in Java and I immediately thought you are an expert. You know all the details behind java language.
@vampiriclion1176
@vampiriclion1176 4 жыл бұрын
.
@entertainshala
@entertainshala 5 жыл бұрын
If we use i
@slinkpot8798
@slinkpot8798 2 жыл бұрын
BRO I have been trying to find a way to do this for class. thank you for the understandable walk thru
@md.shafiqulislam15
@md.shafiqulislam15 9 жыл бұрын
You so much brilliant . Please work with java(OOP) in a Project in which we can easily understand how and when works Inheritance,encaptulation,polymorphism,abstraction ,inheritance and others built in methods from begining to advanced . We will be more greatfull to u.
@gce1904
@gce1904 3 жыл бұрын
Hey buddy, thank you for making this video! For (i < Math.sqrt(n)), I think it must be (i < (Math.sqrt(n) + 1)), so that an i value of Math.sqrt(n) is checked for (n % i) inside the loop. Also, a number less than 2 should be checked for (if(n < 2) { isPrime = false; break;}), because 1 is not a prime number and the for() loop begins with "int i = 2". Otherwise, great video!
@anshraj5916
@anshraj5916 4 жыл бұрын
thank you for this lecture lots of thanks
@kaziarefin2413
@kaziarefin2413 8 жыл бұрын
If you say num/2 than you have to say i
@ktsuw_217
@ktsuw_217 3 жыл бұрын
In here, we are checking whether the number is divisble by itself or 1. If a divides num, there must be b. n/a = b n = a * b Example - Consider number 36 - Find all the factors of 36 that exits in pairs - { a, b}, - {1,36} - {2,18} - {3, 12} - {4, 9} -{ 6, 6} -------------- (a, b} are same. sqrt(n) Pairs are repeated, except a and b positions are swapped. - { 9, 4} - {12, 3} - {18, 2} - {36, 1} {6,6} {a,b} => a^2 = n Therefore, a = sqrt(n) if a < b , a < sqrt(n) , b > sqrt(n) if a > b, a > sqrt(n), b < sqrt(n) From factor pairs of 36, you can see that if you cannot find the factor until sqrt(n), you will not be able to find factors after sqrt of n. Therefore in here Math.sqrt(num) is more efficient compared to num/2 particularly for larger number. Hope this helps.
@xddx2593
@xddx2593 9 жыл бұрын
Sir thanx for this course! it really help me and improve my skills to achieve my task,sir can you make more videos about Swing components like JPopupMenu JMenuItem KeyStrokes KeyEvent i littlebit confuse with these components
@chaitanyavamsi45
@chaitanyavamsi45 7 жыл бұрын
thank u good explanation
@EdClarkHYCT
@EdClarkHYCT 4 жыл бұрын
Excellent video! Great explanation! Keep up the outstanding work!👌👏👍
@GurdeepSabarwal
@GurdeepSabarwal 8 жыл бұрын
it will be better if you explain reasoing behind why restrict loop till num/2 OR Math.sqrt(num) ...thx for sharing .keep it up..
@swarnimkoteshwar
@swarnimkoteshwar 2 жыл бұрын
ikrrr
@daniel11111
@daniel11111 2 жыл бұрын
@@swarnimkoteshwar basic mathematics. To check if a number is prime, all you have to do is to check if any number between 2 and the square root of the number( the one you're checking if its a prime or not) divides evenly into it. Example, square root of 16 is 4, to check if 16 is a prime, you have to check (16 % (1 to 4) )=0. If yes, it is not a prime.
@swarnimkoteshwar
@swarnimkoteshwar 2 жыл бұрын
Thank you so much! This is the exact way I wrote the code in java for prime no. ❤️❤️ Thanks!
@simonasb1208
@simonasb1208 3 жыл бұрын
Excellent video.
@588kumar
@588kumar 5 жыл бұрын
Clean Explanation
@manashranjan1267
@manashranjan1267 6 жыл бұрын
Thanks sir
@md.shafiqulislam15
@md.shafiqulislam15 9 жыл бұрын
At line 12, i,e num%i , i is not decleared as datatype as locally . It shows error. But if declear as local or globally then error shows in break statement. WHY??? Please explain.
@neerajroy9980
@neerajroy9980 6 жыл бұрын
sir your volume so slow and i don't understand boolean value work in this program. Sir please fully explain boolean.
@whagKoppulaSupriya
@whagKoppulaSupriya 3 жыл бұрын
what if the case when user want to give an input which is a 3 digit no. then this code won't work
@anjalifarkase7591
@anjalifarkase7591 3 жыл бұрын
Can I make a program for testing by only using for loop ?
@bharadwaj1613
@bharadwaj1613 7 жыл бұрын
can you keep vedios of selection sort,insertion sort and bubble sort....... can you upload please its urgent for me
@shuvshaw9594
@shuvshaw9594 4 жыл бұрын
very helpful
@Tamil_learning_for_kids
@Tamil_learning_for_kids 7 жыл бұрын
sir, by this concept can we print the random 10 numbers and check it whether its a prime number or not
@codenexa1666
@codenexa1666 3 жыл бұрын
Why you take onece true and false??
@sairajdas6692
@sairajdas6692 6 жыл бұрын
Sir I think it ll not work for 2 .
@gopichandchandu4788
@gopichandchandu4788 8 жыл бұрын
In your video could you please check with number 25 instead of 29 using Math.sqrt(num), for me when i run your program its showing 25 is prime number.
@harsharoyal5514
@harsharoyal5514 4 жыл бұрын
correct
@rovsenhuseynov8368
@rovsenhuseynov8368 4 жыл бұрын
He forgot to write equal (=) sign It must be i
@fahimn6850
@fahimn6850 3 жыл бұрын
Thank you
@plasostories7902
@plasostories7902 6 ай бұрын
Thanks
@pranaydevarashetty1924
@pranaydevarashetty1924 7 жыл бұрын
What is abt package com.navin ???
@abhilipsasatapathy847
@abhilipsasatapathy847 5 жыл бұрын
sir, what is dead code in java
@ashwin6058
@ashwin6058 3 жыл бұрын
sir there is also an another way to do this.. class prime_number { public static void main(String[] args) { int num=100; if(num%2!=0 && num%3!=0 && num%5!=0 && num%7!=0) { System.out.println("prime"); } else { System.out.println("not prime"); }
@shashanksingh1487
@shashanksingh1487 5 жыл бұрын
What if we give input as 0 . It will give result as prime
@regomar
@regomar 8 ай бұрын
but what if the number is 1 or less ?
@ivanpantic6701
@ivanpantic6701 5 жыл бұрын
Telusko, you haven't explained why we use break in this case which is very important. When you use break at the moment the number is successfully divided the operation is stopped an you spend less time. Everyone who do not understand why we have to use it in this case try this code with and without "break;". public class PrimeNumber { /** * @param args the command line arguments */ public static void main(String[] args) { long startTime = System.currentTimeMillis(); int num = 250000000; boolean isPrime = true; for(int i = 2; i < num; i++){ if ( num % i == 0 ){ isPrime = false; break; } } if (isPrime) { System.out.println("number is prime");} else { System.out.println("Number is NOT prime"); } long stopTime = System.currentTimeMillis(); long elapsedTime = stopTime - startTime; System.out.println("Elapsed time is : " + elapsedTime); } }
@shivpatel8288
@shivpatel8288 4 жыл бұрын
What about time complexity???
@NaveenBalaji-fl9vt
@NaveenBalaji-fl9vt 4 жыл бұрын
Sir if the number is 2?
@vaibhav5630
@vaibhav5630 3 жыл бұрын
The condition should be i
@subhojeetsinha7865
@subhojeetsinha7865 7 жыл бұрын
import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { int num, i, count=0; Scanner scan = new Scanner(System.in); System.out.print("Enter a Number : "); num = scan.nextInt(); for(i=2; i
@theabhishekmittal
@theabhishekmittal 6 жыл бұрын
what is count
@theabhishekmittal
@theabhishekmittal 6 жыл бұрын
why u useed count++ when u have i++ over here
@rajeshmallick6636
@rajeshmallick6636 7 жыл бұрын
sir i need your help i want to print prime number between 1 to 100
@sonusharma6580
@sonusharma6580 6 жыл бұрын
package Prime_Number; public class PrimeNumber { public static void main(String[] args) { int number=100; for (int i=2;i
@satyapalsingh7487
@satyapalsingh7487 5 жыл бұрын
@@sonusharma6580 galat hai
@s.m.alinaqvi5142
@s.m.alinaqvi5142 5 жыл бұрын
import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int num=sc.nextInt(); for (int i = 2; i
@downloaderhdvideomohamed747
@downloaderhdvideomohamed747 4 жыл бұрын
29 is a prime number when the loop counter reaches 29. But for you have put a condition if(number%i==0){isprime = false; break;} so this is a problem my dear sir.
@desigeekchishi
@desigeekchishi 3 жыл бұрын
Why flag is used ?
@prosunchakraborty92
@prosunchakraborty92 8 жыл бұрын
what is the purpose of break statement here ?
@pawan208402
@pawan208402 7 жыл бұрын
because if it gets divide then it will return false and thats what we want. n after getting that why to go further simply BREAK it n will come out
@akshayabrol1026
@akshayabrol1026 6 жыл бұрын
Poor sound quality. Always speak louder
@entertainshala
@entertainshala 5 жыл бұрын
I think it should be i
@ramadanelnaggar5126
@ramadanelnaggar5126 5 жыл бұрын
navin scond code(For (int i = 2 ; i < num/2 ; i++) { Is prime = false; })and the third one (For (int i = 2 ; i
@veeracilambanv6464
@veeracilambanv6464 5 жыл бұрын
After declare n/2 in for loop its showing 4 is a prime number
@subhajitshit4274
@subhajitshit4274 4 жыл бұрын
nice program and coding application link send
@libralhindu6753
@libralhindu6753 Жыл бұрын
👌👌👌
@abhilashpotharaju9390
@abhilashpotharaju9390 7 жыл бұрын
while using Math.sqrt , it should be i
@RandomDays906
@RandomDays906 6 жыл бұрын
Abhilash Potharaju it would actually print any square of a prime as a prime.
@yashsati29
@yashsati29 7 жыл бұрын
1.09 public class PrimeNumberCheck { public static void main(String[] args) { int number; number=12; for(int i=2;i
@asimshahzad4998
@asimshahzad4998 7 жыл бұрын
in this case it will print as many times as loop iterates ... so to prevent this he uses boolean function
@MKSRR1
@MKSRR1 6 жыл бұрын
maybe to print true or false.. but it can also be done like " Number is NOT Prime hence false". still ya ur right
@RockStar-ll8qp
@RockStar-ll8qp 5 жыл бұрын
You have not put braces after else. So the break statement is not in the else block and will be executed in the first iteration itself and the loop will terminate.
@madhanagopalselvaganapathi8341
@madhanagopalselvaganapathi8341 2 жыл бұрын
First of all your coding is wrong , 2 is a prime number
@Unknown-rt9vl
@Unknown-rt9vl Жыл бұрын
U r right
@pawan208402
@pawan208402 7 жыл бұрын
does isPrime built in function
@kush5333
@kush5333 6 жыл бұрын
"isprime" is Boolean type variable
@kush5333
@kush5333 6 жыл бұрын
You can change it to "primeis" or anything you wanna use ^-^
@shuvshaw9594
@shuvshaw9594 4 жыл бұрын
int x=290; boolean var=true; for(int i=1; i
@NeelSandellISAWESOME
@NeelSandellISAWESOME 8 жыл бұрын
why did you put i++
@pawan208402
@pawan208402 7 жыл бұрын
to go for every no upto 25
@venkateswarluguntaka7370
@venkateswarluguntaka7370 4 жыл бұрын
@Thegamer-yp7qq
@Thegamer-yp7qq 8 жыл бұрын
I didn't understand what % doing?
@binukashihan4480
@binukashihan4480 4 жыл бұрын
That is mod.. Remainder when dividing eg : - 5 % 2 = 1
@kanharajput5443
@kanharajput5443 3 жыл бұрын
int k=1 , n=7, sum=0; while(k
@jenish19188
@jenish19188 Жыл бұрын
Here is the correct solution that covers negative as well as number 2 : import java.util.Scanner; public class NumIsPrimeOrNot { public static void main(String[] args) { //Question: is given number is prime or not? return boolean //Prime check: //1) number should be Natural number > 1 (Note: 1 is not a prime but 2 is prime number //2) number should have only 2 factors. Means divisible by only 1 and the number itself. Scanner sc = new Scanner(System.in); System.out.println("Enter any number : "); int num = sc.nextInt(); boolean isPrime = true; if (num
@nagrajkeshwar3306
@nagrajkeshwar3306 2 жыл бұрын
plzzzzz improve your audio its very low audiable
@theabhishekmittal
@theabhishekmittal 6 жыл бұрын
sir you are teaching as if people already knows java. these videos are not helpful to those who doesnt kows java..
@neerajjoshi4704
@neerajjoshi4704 6 жыл бұрын
You must go through this book for complete knowledge in java "Complete refrence in Java"
@abelashenafi6291
@abelashenafi6291 3 жыл бұрын
Let me share with you guys what I've come up with after a while of coding and debugging. Let me know if this was helpful. public class Is_NPrime { public static int IsPrime (int N) { int Dividers = 0; int ReturnValue = 1; for (int i = 1; i < N; i++) { if (N % i == 0) Dividers ++; if (Dividers > 1) // Break from the loop as soon as possible!! { ReturnValue = 0; break; } } return ReturnValue; } public static void main(String[] args) { System.out.println (IsPrime (12)); } }
@ayushsawadh2598
@ayushsawadh2598 2 жыл бұрын
The true art of coding is how you can code things in a easy way so that humans can also understand. And what you try to do here is just making it more complex. So keep this in mind and try to make things easy.
@varshapatil5263
@varshapatil5263 5 жыл бұрын
Public class prime { public static void main(string[] args) { int n=20; If(n%2==0) System.out.println("even"); else System.out.println("odd"); } }
@shrutim3234
@shrutim3234 Жыл бұрын
poor sound quality
@aryanverma8324
@aryanverma8324 5 жыл бұрын
Very dirty sound quality!!!
@gravity6822
@gravity6822 Жыл бұрын
bruh...u just called us guyz... i didnt expect this frm u vro..(no one cares lol)
@cricketkingA.S
@cricketkingA.S Жыл бұрын
bakvas
@pkeditsff8405
@pkeditsff8405 Ай бұрын
Worst method 😢😢😢😢
Find Prime Numbers In Java - Full Walkthrough with Source
13:17
Coding with John
Рет қаралды 35 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 39 МЛН
Опасность фирменной зарядки Apple
00:57
SuperCrastan
Рет қаралды 12 МЛН
Jumping off balcony pulls her tooth! 🫣🦷
01:00
Justin Flom
Рет қаралды 29 МЛН
Java Program #10 - Palindrome Number in Java
10:03
Programming For Beginners
Рет қаралды 12 М.
Functional Interface | Lambda Expression in Java
13:56
Telusko
Рет қаралды 143 М.
Check If A Number Is Prime | C++ Example
8:29
Portfolio Courses
Рет қаралды 17 М.
Array vs. ArrayList in Java Tutorial - What's The Difference?
17:36
Coding with John
Рет қаралды 511 М.
How I would learn to code (If I could start over)
9:16
Jason Goodison
Рет қаралды 4,7 МЛН
Java is ALWAYS Pass By Value. Here's Why
5:22
Coding with John
Рет қаралды 120 М.
Java Program #6 -  Find Prime Numbers in Java
8:49
Programming For Beginners
Рет қаралды 7 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 39 МЛН