How to find Prime Number || Basic Programming Questions Series

Поділитися
Вставка
  • Опубліковано 18 жов 2024

КОМЕНТАРІ • 38

  • @manishpatidar5379
    @manishpatidar5379 5 років тому +4

    perfect coding with all negative test cases

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

    Naveen you are a maverick.

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

    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???

  • @jayalakshmig3465
    @jayalakshmig3465 5 років тому +2

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

  • @sarakhalifa9928
    @sarakhalifa9928 8 місяців тому

    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

  • @diptmangupta2385
    @diptmangupta2385 5 років тому +2

    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 ?

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

      Just think about no. 25, it won't be divisible by 2, but divisible by 5.

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

      This is correct. It is an optimization. There is no sense testing values > n/2. The example for 25 will stop testing well before 12, as it will find it is not prime at 5.

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

      @@edwarddurkin6635 we can even further optimize by limiting loop till Sqrt(n) and we get same result

  • @igoril2010
    @igoril2010 2 роки тому +1

    @Naveen AutomationLabs , again huge thanks ! Iknow u didnt use it here but still : if in isPrime() i say i

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

    One question Naveen, i saw some programs on internet for checking prime numbers and there logic is including i

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

      if you put number 9... it gives you prime and that's wrong...

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

      Its for optimization of program. By doing i

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

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

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

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

  • @manikolla993
    @manikolla993 5 років тому +2

    Your genius 👏🏻

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

    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

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

    best explanation, i can say ,,

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

    Always best

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

    Explanation 👌

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

    Yes...u should cover

  • @AmitYadav-c2s8r
    @AmitYadav-c2s8r 7 місяців тому

    class HelloWorld {
    public static boolean isPrime(int num)
    {
    if(num

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

    how to print prime numbers by using scanner class?

  • @myamazingvideos
    @myamazingvideos 5 років тому +2

    /**
    * 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 ->
    ");
    listPrimenumbers(i);
    System.out.println("
    ");
    } 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 ->
    ");
    listPrimenumbers(i);
    System.out.println("
    ");
    }
    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 ->
    ");
    listPrimenumbers(j);
    System.out.println("
    ");
    } 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 ->
    ");
    listPrimenumbers(j);
    System.out.println("
    ");
    }
    }
    public static boolean isPrime(int num) {
    if (num
    2,3,5,7,11,13,17,19,23,
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Thanks @Naveen

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

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

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

    Thanks thanks

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

    Why do u declare the method as static ?

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

      So that we do not have to create an object, further do not have to call that method with the object reference variable in main method.
      So if the method is static then we can directly call it in main method by its name.

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

    for 9 its showing true how

  • @balajibalu7390
    @balajibalu7390 5 років тому +2

    If i use number 9 can anyone explain

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

      that's the same question I have, 9 is not prime but the logic accepts it

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

      I noticed while working on the logic that people are forgetting that if(num%i==0) is inside a for loop, where i value is incremented.
      So for example, num=25
      25%2==1: fails and then i value is increased to 3
      25%3==1: fails again, i=4
      25%4==1: fails again, I=5
      25%5==0: if condition is true and executes the return false statement and hence 25 is not a prime number.
      Basically one has to be clear with loops and if-else concepts as suggested by Naveen here.😅

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

    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

    • @naveenautomationlabs
      @naveenautomationlabs  5 років тому +6

      15%2==1 not 0. First clear your basic concepts.

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

      @@naveenautomationlabs
      How to find for number 25

    • @harinderkaur3040
      @harinderkaur3040 4 роки тому +3

      I noticed while working on the logic that people are forgetting that if(num%i==0) is inside a for loop, where i value is incremented.
      So for example, num=25
      25%2==1: fails and then i value is increased to 3
      25%3==1: fails again, i=4
      25%4==1: fails again, I=5
      25%5==0: if condition is true and executes the return false statement and hence 25 is not a prime number.
      Basically one has to be clear with loops and if-else concepts as suggested by Naveen here.😅

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

      @@harinderkaur3040 thank you, that explains why it works.