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

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 96

  • @kaziarefin2413
    @kaziarefin2413 8 років тому +7

    If you say num/2 than you have to say i

    • @ktsuw_217
      @ktsuw_217 4 роки тому +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.

  • @SmartProgramming
    @SmartProgramming 6 років тому +2

    nice tutorial sir, some of them are so helpful, thanks and keep it up 👍👍

    • @vampiriclion1176
      @vampiriclion1176 5 років тому

      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 5 років тому

      .

  • @gce1904
    @gce1904 3 роки тому +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!

  • @GurdeepSabarwal
    @GurdeepSabarwal 8 років тому +9

    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 роки тому +1

      @@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 роки тому +1

      Thank you so much! This is the exact way I wrote the code in java for prime no. ❤️❤️ Thanks!

  • @santoshraj219
    @santoshraj219 4 роки тому +7

    It has a bug, if someone enter 2 as value it will return as not prime

  • @xddx2593
    @xddx2593 9 років тому +7

    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

  • @ivanpantic6701
    @ivanpantic6701 5 років тому +1

    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);
    }

    }

  • @jesuslovesyou2270
    @jesuslovesyou2270 8 років тому +1

    this is the most helpful vid I found to solve this problem. thank u so much sir

  • @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 років тому +1

    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.

  • @subhojeetsinha7865
    @subhojeetsinha7865 7 років тому +5

    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

  • @entertainshala
    @entertainshala 6 років тому +2

    If we use i

  • @588kumar
    @588kumar 5 років тому

    Clean Explanation

  • @ashwin6058
    @ashwin6058 4 роки тому +1

    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");
    }

  • @gopichandchandu4788
    @gopichandchandu4788 8 років тому +4

    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.

  • @simonasb1208
    @simonasb1208 4 роки тому

    Excellent video.

  • @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

  • @neerajroy9980
    @neerajroy9980 6 років тому +2

    sir your volume so slow and i don't understand boolean value work in this program. Sir please fully explain boolean.

  • @shuvshaw9594
    @shuvshaw9594 5 років тому

    int x=290; boolean var=true;
    for(int i=1; i

  • @yashsati29
    @yashsati29 8 років тому +4

    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

  • @chaitanyavamsi45
    @chaitanyavamsi45 8 років тому +1

    thank u good explanation

  • @EdClarkHYCT
    @EdClarkHYCT 4 роки тому

    Excellent video! Great explanation! Keep up the outstanding work!👌👏👍

  • @Tamil_learning_for_kids
    @Tamil_learning_for_kids 8 років тому +1

    sir, by this concept can we print the random 10 numbers and check it whether its a prime number or not

    • @codenexa1666
      @codenexa1666 4 роки тому

      Why you take onece true and false??

  • @anshraj5916
    @anshraj5916 5 років тому

    thank you for this lecture lots of thanks

  • @jenish19188
    @jenish19188 2 роки тому

    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

  • @manashranjan1267
    @manashranjan1267 6 років тому +2

    Thanks sir

  • @regomar
    @regomar Рік тому

    but what if the number is 1 or less ?

  • @anjalifarkase7591
    @anjalifarkase7591 4 роки тому

    Can I make a program for testing by only using for loop ?

  • @abhilashpotharaju9390
    @abhilashpotharaju9390 7 років тому +3

    while using Math.sqrt , it should be i

    • @RandomDays906
      @RandomDays906 7 років тому

      Abhilash Potharaju it would actually print any square of a prime as a prime.

  • @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.

  • @downloaderhdvideomohamed747
    @downloaderhdvideomohamed747 5 років тому

    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.

  • @akshayabrol1026
    @akshayabrol1026 6 років тому +12

    Poor sound quality. Always speak louder

  • @plasostories7902
    @plasostories7902 11 місяців тому

    Thanks

  • @sairajdas6692
    @sairajdas6692 6 років тому +2

    Sir I think it ll not work for 2 .

  • @kanharajput5443
    @kanharajput5443 4 роки тому

    int k=1 ,
    n=7, sum=0;
    while(k

  • @shuvshaw9594
    @shuvshaw9594 5 років тому

    very helpful

  • @entertainshala
    @entertainshala 6 років тому +1

    I think it should be i

    • @ramadanelnaggar5126
      @ramadanelnaggar5126 6 років тому

      navin scond code(For (int i = 2 ; i < num/2 ; i++)
      {
      Is prime = false;
      })and the third one (For (int i = 2 ; i

  • @vaibhav5630
    @vaibhav5630 3 роки тому +1

    The condition should be i

  • @rajeshmallick6636
    @rajeshmallick6636 8 років тому +1

    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 6 років тому

      @@sonusharma6580 galat hai

  • @bharadwaj1613
    @bharadwaj1613 8 років тому +1

    can you keep vedios of selection sort,insertion sort and bubble sort....... can you upload please its urgent for me

  • @shashanksingh1487
    @shashanksingh1487 6 років тому

    What if we give input as 0 . It will give result as prime

  • @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

  • @desigeekchishi
    @desigeekchishi 4 роки тому

    Why flag is used ?

  • @abhilipsasatapathy847
    @abhilipsasatapathy847 5 років тому

    sir, what is dead code in java

  • @shivpatel8288
    @shivpatel8288 4 роки тому

    What about time complexity???

  • @veeracilambanv6464
    @veeracilambanv6464 6 років тому +1

    After declare n/2 in for loop its showing 4 is a prime number

  • @prosunchakraborty92
    @prosunchakraborty92 9 років тому

    what is the purpose of break statement here ?

    • @pawan208402
      @pawan208402 8 років тому +1

      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

  • @pranaydevarashetty1924
    @pranaydevarashetty1924 7 років тому

    What is abt package com.navin ???

  • @theabhishekmittal
    @theabhishekmittal 7 років тому +5

    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 роки тому +2

    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 3 роки тому

      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.

  • @NaveenBalaji-fl9vt
    @NaveenBalaji-fl9vt 5 років тому

    Sir if the number is 2?

  • @pawan208402
    @pawan208402 8 років тому

    does isPrime built in function

    • @kush5333
      @kush5333 7 років тому

      "isprime" is Boolean type variable

    • @kush5333
      @kush5333 7 років тому

      You can change it to "primeis" or anything you wanna use ^-^

  • @libralhindu6753
    @libralhindu6753 2 роки тому

    👌👌👌

  • @Thegamer-yp7qq
    @Thegamer-yp7qq 9 років тому +3

    I didn't understand what % doing?

    • @binukashihan4480
      @binukashihan4480 5 років тому

      That is mod..
      Remainder when dividing
      eg : - 5 % 2 = 1

  • @NeelSandellISAWESOME
    @NeelSandellISAWESOME 8 років тому

    why did you put i++

    • @pawan208402
      @pawan208402 8 років тому

      to go for every no upto 25

  • @subhajitshit4274
    @subhajitshit4274 4 роки тому

    nice program and coding application link send

  • @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");
    }
    }

  • @nagrajkeshwar3306
    @nagrajkeshwar3306 3 роки тому

    plzzzzz improve your audio its very low audiable

  • @venkateswarluguntaka7370
    @venkateswarluguntaka7370 4 роки тому

  • @aryanverma8324
    @aryanverma8324 6 років тому +2

    Very dirty sound quality!!!

  • @shrutim3234
    @shrutim3234 2 роки тому

    poor sound quality

  • @pkeditsff8405
    @pkeditsff8405 7 місяців тому

    Worst method 😢😢😢😢

  • @gravity6822
    @gravity6822 2 роки тому

    bruh...u just called us guyz... i didnt expect this frm u vro..(no one cares lol)

  • @GamingwithNadebaaz
    @GamingwithNadebaaz Рік тому

    bakvas

  • @fahimn6850
    @fahimn6850 3 роки тому

    Thank you