Special Programs in C − Check If The Number Is Prime Number

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

КОМЕНТАРІ • 117

  • @ziko9762
    @ziko9762 Рік тому +22

    A simpler code: you just need to divide the number by individual elements of the for loop from 1 to that number and count each iteration. When the successful count is only two, it's a prime number
    #include
    int main()
    {
    int a, b, count = 0;
    printf("Enter value: ");
    scanf("%d", &a);
    for (b = 1; b

    • @fenggao2213
      @fenggao2213 Рік тому +11

      Good suggestion. But one thing to notice is that the solution given in the video runs in O(sqrt(n)) whereas your solution runs in O(n). While O(n) is efficient enough, it still leads to a huge performance difference when the input is big enough.

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

      Super bro

    • @Kunal-yg9fs
      @Kunal-yg9fs 4 місяці тому +2

      that code is not at all efficient... suppose I enter the value 10000 then you program loop will run 10000 times which is unnecessary and useless bcz we know that the factor of any number is present within the square root range of that number..

    • @AshishKhetwal
      @AshishKhetwal Місяць тому

      your program is less efficient and less optimized otherwise this is definitely correct but later on we need to make programs to avoid time complexity

  • @SVNIT29
    @SVNIT29 Рік тому +7

    We can also write the program in this way -
    Printf("Thanks Neso Academy");

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

    A VERY BETTER SOLUTION
    #include
    #include
    int main()
    {
    int a,rem,i,d,e;
    printf("Enter the number:");
    scanf("%d",&a);
    if(a==1)
    {
    printf("It is neither prime nor composite");
    exit(0);
    }
    for(i=2;i

  • @hmnems8202
    @hmnems8202 3 роки тому +2

    this method works too:
    int num, counter = 0;
    scanf("%d", &num);
    for(int i = 1; i

  • @sksahifali8769
    @sksahifali8769 2 роки тому +5

    I think it is quite difficult for beginners like us.
    I found the below code much easier(for me) than the code in the video.
    #include
    int main()
    {
    int n, i, count=0;
    printf("Please enter a number:");
    scanf("%d",&n);
    for(i=1;i

  • @ritwikasanyal9939
    @ritwikasanyal9939 4 роки тому +12

    In the last if else statement val2==3 condition was not necessary because when val2=3 ,val1=2 and hence count is becoming zero...so it is becoming a prime number...

    • @thewolverine5591
      @thewolverine5591 3 роки тому +2

      yes,3 is no needed, thank you for the information

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

      I was thinking the same

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

      But can you tell me why we are calculating square root here????

  • @Abhishek-yf7wi
    @Abhishek-yf7wi 2 роки тому +2

    This works for all the cases. But the problem is it is not optimal for large values of input. As studied in asymptotic analysis, this will take a long time to compute the result. But works fine for small numbers.
    int main()
    { int n,a,count=0;
    printf("Enter a number
    ");
    scanf("%d",&n);
    for(int i=1;i

  • @funcrazygaming6410
    @funcrazygaming6410 4 роки тому +5

    Thank You Very Much Sir For Your Efforts But I Have Another Solution More Short Than Yours I Hope That You Will Evaluated And Tell Me If I made Some Mistakes :
    int num;
    printf("Enter Your Number Please !! :");
    scanf("%d",&num);
    int count = 0;
    for (int i=1;i

  • @027krishnadeshmukh9
    @027krishnadeshmukh9 5 років тому +3

    Sir this is my code for the problem:
    I find it simple. Are there any flaws?
    #include
    #include
    #include
    int main()
    {int i=1,q,count=0;
    printf("type in your number");
    scanf("%d",&q);
    while(i

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

      #include
      int main()
      {
      int a,b,i;
      printf("enter a no. :");
      scanf("%d",&a);
      for(i=2;i

  • @karthikdurai8209
    @karthikdurai8209 3 роки тому +11

    I think if((count==0 && val2!=1) || (val2==2) ||(val2==3)) in this condition val2==3 is not needed...

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

    //This also works fine and much more simple and ease to understand
    #include
    #include
    int main()
    {
    int num,flag,c;
    printf("Enter a number: ");
    scanf("%d", &num);
    for(int i=2;i

  • @G1Joshi
    @G1Joshi 5 років тому +31

    Today, I learn a new thing...
    "Every positive integer greater than one can be written uniquely as the product of primes."

    • @rsingh6216
      @rsingh6216 3 роки тому +5

      Neso is great bro.

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

      bhai ye class 6 mein hota h

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

      @@uraharakisuke5305 haan, class 6 ka questions UPSC exams me b atta he.. kisi b knowledge ko chota ya bara maat samjo..

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

      @@andistheinforitbutso7513 pata hai bhai maine woh nahi bola...maine just bola ki ye matlab school mein bhi padhaya jaata hai koi out of the world cheez nahi hai...

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

    You can add an if condition after scanf i.e if (x > 0) with else printf("You have entered 0 or Negative number"); at the end of the code. Where x is input from user. Covers 0 and negative numbers from being printed as prime

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

    --what do you think guys
    int number;
    printf("enter a number to check if it's a prime or composite ");
    scanf("%d",&number);
    if(number==0||number==1)
    printf("either");
    else if(number%2==0&&number!=2)
    printf("this number : %d is a composite number",number);
    else
    printf("this number : %d is a prime number",number);

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

    #include
    int main()
    {
    int n,i,count=0;
    printf("enter a number=");
    scanf("%d",& n);
    for(i=1;i

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

    You could do it easily
    #include
    int main()
    {
    int a, b, count = 0;
    printf("Enter a number
    ");
    scanf("%d", &b);
    for( a = 2; a < b; a++)
    {
    if( (b%a) == 0 )
    {
    count = 1;
    break;
    }
    }
    if( count == 0 )
    printf(" is prime number");
    else
    printf(" is not a prime number");
    return 0;
    }

  • @rishabhverma5848
    @rishabhverma5848 4 роки тому +6

    why are we checking for 3 as well , 3 should be included in the loop . plz help

  • @YasirAli-pk8de
    @YasirAli-pk8de 4 роки тому +6

    Program to check prime numbers between two numbers with the same trick:-
    int main()
    {
    int a,b,c,d,e,count;
    printf("enter two numbers");
    scanf("%d%d",&a,&b);
    for(c=a;c

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

    The best explanation one can get on you tube

  • @VikashKumar-kb8jh
    @VikashKumar-kb8jh 5 років тому +7

    Well explained Sir but I would I loved it if you could have explained the logic of taking √n

    • @Buddharta
      @Buddharta 4 роки тому +4

      I had the same question and a friend pointed out to the solution:
      If n is not prime then there are two positive integers a and b such that:
      n = ab
      If a = b then n = a² and a is the root
      If a != b then either a

    • @VikashKumar-kb8jh
      @VikashKumar-kb8jh 4 роки тому

      @@Buddharta thanks buddy understood

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

      @@Buddharta
      Bro
      can you explain by giving integer examples?
      Why we are square root the number first?

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

      @@mdf1259 sure, bro.
      For every non prime there is an interger product that expresses it, for example:
      6=2x3
      20=2x10
      9=3x3
      27=3x9
      Every divisor comes in pairs since you need two numbers to make a product, if you see in the examples above, (2,3) (2,10) (3,3)(3,9) at least one is samller than the square root and if you have one you have the other by dividing

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

      @@Buddharta
      Bro
      I didn't get it.
      Still had a dought
      For non prime two product needs that is ok.
      Then what is your logic ?
      Thanks for your effort and time!

  • @engmoatazabdelhalim7343
    @engmoatazabdelhalim7343 17 днів тому

    Thank you very much
    I think
    val2=x; is not necessary
    Also the condition ( val2==3) is not necessary

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

    I just had a question to ask why val2 (another variable) is taken though we could use x instead here... I mean just taking one variable could reduce size of code
    OR, there are some reasons behind to do so?

    • @Name-is2bp
      @Name-is2bp 3 роки тому +1

      yes i just asked the same question!
      did you find the answer?? i really want to know.

  • @TusharJaiswal-v2w
    @TusharJaiswal-v2w 5 місяців тому

    easy code : #include
    #include
    int main()
    {
    int number;
    int count = 0;
    printf("Enter a Number : ");
    scanf("%d", &number);
    if (number

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

    I did pl/SQL program with this logic

  • @98_shubhamr.sharma78
    @98_shubhamr.sharma78 2 роки тому +7

    Sir please explain Step 2 and Step 3 in more detail, difficult for beginners like me to grasp

  • @TigerChess2
    @TigerChess2 4 роки тому +4

    Why don't we divide the number first by 2? Then we don't have to divide the number by 3 to sqrt(x). We could simply divide the number by 2. Then we have to only divide the number by odd numbers.

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

    If user enters 0, the output is that it's a prime number. You must add in part 3 "..&& val2 != 0"

    • @sudhirammeena8068
      @sudhirammeena8068 4 роки тому +5

      sir firstly tell us that only enter a positive number but zero is a non positive number

  • @102ayushkumar3
    @102ayushkumar3 4 роки тому

    I tried this on my own...it is working.
    #include
    int main() {
    int n,x=2,flag=1;
    printf("Enter an integer: ");
    scanf("%d", &n);
    while(n!=x){
    if(n%x==0 || n==1){
    printf("%d is not a prime number.", n);
    flag = 0;
    break;
    }
    x++;
    }
    if(flag==1) printf("%d is a prime number.", n);
    return 0;
    }

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

    And you can set a "BREAK" when the result of (val2%i) ==0. It will jumb of the loop

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

      The printf part won't be evaluated...suppose val2 = 2; and i = 2;

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

    Superb sir😃

  • @ДаниилКоршков-г8ш

    Did it via Ferment Little Theorem, as it is way faster. Was making a toy RSA with small key length, and had to make algorithm to find primes

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

    Thank you bro you helped me so much

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

    thank you for your presentation
    do we want to add break ; after count == 1 ; ?
    beacause if we found one devisible number , so we don't need to check remaining numbers ??
    for (i = 2; i

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

    C program to check whether a number isPrime or not:
    #include
    #include
    int main()
    {
    int x, v;
    scanf("%d", &x);
    v = 2;
    bool isPrime = true;
    while (v

  • @vijaya7170
    @vijaya7170 5 років тому +3

    Thank u so much sir u are really awesome I can understand each step carefully😃

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

    #include
    #include
    int main(void) {
    int number;
    printf("Enter number: ");
    scanf("%d", &number);
    int isPrime = 1; // Assume the number is prime initially
    // Check divisibility up to square root of the number
    for (int i = 2; i

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

    main()
    int x,i;
    printf("enter a number");
    scanf("%d",&x);
    for(i=2;i

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

    Thank you sir😊

  • @NobodyKnows098
    @NobodyKnows098 2 роки тому +2

    I don't understand why you write val2 = x.

  • @dineshkannan564
    @dineshkannan564 5 років тому +3

    Sir I don't understand about value1 initialisation can any one explain

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

    Great! Best ways and easy ways. Expecting more from you.Specially for campus recruitement test,like tcs

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

    Thank u

  • @AakashYadav-ch9un
    @AakashYadav-ch9un 4 роки тому +2

    Why 3 is a special case
    I think it will be calculated

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

    Best C programming lectures, sir plzz upload video lectures of C++

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

    please do upload java tutorials video.

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

      Have you got java tutorials

  • @Abdulshakoor-p1s9d
    @Abdulshakoor-p1s9d 10 місяців тому

    Why can we just not use an else condition in which (if i num modulus i ==O) then print non prime
    Otherwise in else condtion print prime as its modulus was zero

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

    what is time complexity of this program? is it better than sieve of erastothenes?

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

    Sir your explanation is very good.
    Thanks for teaching us.

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

    Sir I have a question....
    Agar hum itna sara code likhne ki jagah pe sirf k loop chala k apne entered no. "n" ko 2 to n tak divisibility check kr le aur ek break use kare toh easy nhi ho jayega ye sab

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

      Code execution time pdh Jayega kyuki loop phir boht Baar chlega

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

    He is teaching Complicated programs it's very easy compare to what he teaching

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

    U can add break in if func

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

    Why are we setting the limit to sqrt(x)?

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

    Seriously this program didn't work I had to modify the code to get this but your logic was correct.

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

    I don't understand why we put x into val2. I've replaced val2 simply with x and the program works fine. am I missing something?

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

      maybe it just for precaution? what i know is tht we need to do tht if the operation going to make change to the ori value.

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

    The input of number 3 is covered by the loop baby...lol

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

    int n, i, c = 0;
    printf("Enter any number n:");
    scanf("%d", &n);
    //logic
    for (i = 1; i

  • @MariaPaula-fc3mh
    @MariaPaula-fc3mh 4 роки тому

    why if a user enter the number 2 and 3 we printf directly the number is prime what about other numbers? like 5 ,7..etc.isnt much easier to write if (2 or 3 or 5 or 7 or 11..etc) printf number is prime and for other bigger numbers we do the checking?

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

      Sure, just name it "Program to print prime numbers" 🤓🤓

  • @YoYo-nt7yf
    @YoYo-nt7yf 4 роки тому

    Your accent is very nice to the ears.
    I cant listen to others vid. lectures with bad accent.

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

    How we can write 7 as a product of two primes?

  • @techno-track318
    @techno-track318 Рік тому

    Why we check 2 & 3 in part 3

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

    My program for this problem:
    #include
    #include
    //Compiler version gcc 6.3.0
    int main()
    {
    int n, i, x;
    printf("Enter a number: ");
    scanf("%d", &n);
    if(n == 0 || n == 1 || n == 3)
    {
    printf("%d is not a prime number",n);
    exit(0);
    }
    x = sqrt(n);
    for(i=2; i

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

    4 shows up as a prime number

    • @Name-is2bp
      @Name-is2bp 3 роки тому

      it worked fine for me, re-check your code. :)

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

    sir when i am trying to run these functions in my compiler it is showing error plz tell me why...

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

      Try to change last conditional statement to:
      if(count>0)
      printf(“Composite”);
      else
      printf(“Prime”);
      You can also check for i/p 0 or 1 at first and print neither prime nor composite

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

    How to find largest 9 digit prime number in c program

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

    Best Easiest code of check prime number or not is written below please check it!
    #include
    int main()
    {
    int i,count=0,number;
    printf("Enter your number: ");
    scanf("%d",&number);
    for(i=2; i

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

    👍👍

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

    Square root without math.h.It is possible put a video

  • @reflexop1566
    @reflexop1566 9 місяців тому

    Chipi chipi chapa chapa dubi rubi daba daba magic poni dubi rubi bum bum buk

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

    This code says that 0 is a prime no.

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

    Ist Viewer 😍

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

    pretty bad and inefficient way of checking

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

      The algorithm will look something along the lines of :
      prime = 1
      for i=2 to (n/2) {
      if(n % i == 0){
      prime = 0
      break
      }
      }
      if(prime){
      print n is prime
      }
      else{
      print n is not prime
      }
      Less computation, no special cases or anything.
      EDIT: I've completely missed the sqrt() part in the video. It is already computationally efficient. You just need a flag to decrease typographical complexity then.

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

      No it doesn't say 4 is a prime number. At the first iteration, 4 is divisible by 2 and the reminder is 0. So the flag is reset.
      And yes 1 is the only special case.
      Also, I have written about sqrt (n) in the previous post, see the 'EDIT' part .

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

      Yup that was my mistake. Was in a hurry. And I've never said you're wrong. And was never in fear to admit my mistakes too. I said there is a better way. And yes, what you're doing is very helpful indeed.

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

      The neso academy has taken time complexity in consideration while writing the program on the other hand there are different methods for finding whether a number is prime or not but this is the most efficient one.

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

      @@subhranil_mukherjee your way is much more inefficient. If you pick large 3 digit numbers like 659 (tested to be a prime number), then you would be testing up to the 300s (n/2) to see if it is prime. You only have to test up to the square root, which is literally up to 26 (much fewer cases). The reason you find the square root is because if there is a pair of factors other than 1 and the number you are testing, the square root would be the center point of two factors being multiplied. Take 26, you don't need to test all the way to 13 because anything below 6 will be multiplied with a number higher than 6 since 6 is the approximate central integer factor. You start at 2, and you've already found out it works because 2 and 13 work. This happens the same way as you get to larger and larger integers.

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

    #include
    int main()
    {
    int x,count,i;
    printf("\tEnter a positive natural number = ");
    scanf("%d",&x);
    count = 0;
    for(i=1;i

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

    #include
    int main() {
    int num = 0;
    scanf("%d",&num);
    if(num == 1)
    {
    printf("Not prime number");
    return 0;
    }
    for(int i = 2; i

  • @ABHISHEKMISHRA-yp1jn
    @ABHISHEKMISHRA-yp1jn 5 місяців тому

    i have shortest method
    #include
    int main(){
    int n;
    int isprime=1;
    printf("enter the number:");
    scanf("%d",&n);
    for(int i=2;i

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

    Whyy not this ?
    #include
    int main() {
    int n ;
    printf("Enter any positive integer :");
    scanf("%d", &n);
    if (n==2) {
    printf("It is a Prime number"); }
    else if (n%2 != 0) {
    printf("It is a Prime number"); }
    else printf("It is a non-Prime number");
    return 0;
    }

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

    #include
    int main(){
    int n,i,count=0,d;
    printf("enter the no :");
    scanf("%d",&n);
    for(i=1;i

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

    #include
    int main() {
    int n, rem, p;
    int flag = 1;
    printf("The number :");
    scanf_s("%d", &n);
    p = n;
    for (int i = 2; i < p; i++)
    {
    rem = p % i;
    if (rem == 0)
    {
    flag = 0;
    break;
    }
    }
    //printf("%d
    ", flag);
    if (flag)
    printf("The number Is prime Number");
    else
    printf("The number is not prime Number");
    return 0;
    }

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

    Thank u