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

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 มิ.ย. 2024
  • C Programming & Data Structures: Special C Programs − Check If The Number Is Prime Number.
    Topics discussed:
    1) Prime numbers.
    2) Composite numbers.
    3) C program for prime numbers.
    C Programming Lectures: goo.gl/7Eh2SS
    Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
    Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
    Contribute: www.nesoacademy.org/donate
    Memberships: bit.ly/2U7YSPI
    Books: www.nesoacademy.org/recommende...
    Website ► www.nesoacademy.org/
    Forum ► forum.nesoacademy.org/
    Facebook ► goo.gl/Nt0PmB
    Twitter ► / nesoacademy
    Music:
    Axol x Alex Skrindo - You [NCS Release]
    #CProgrammingByNeso #CProgramming #PrimeNumberProgram

ความคิดเห็น • 112

  • @ziko9762
    @ziko9762 ปีที่แล้ว +21

    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.

    • @user-ns5pg8vu5j
      @user-ns5pg8vu5j 7 หลายเดือนก่อน

      Super bro

  • @karthikdurai8209
    @karthikdurai8209 3 ปีที่แล้ว +11

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

  • @ritwikasanyal9939
    @ritwikasanyal9939 3 ปีที่แล้ว +10

    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 3 หลายเดือนก่อน

      I was thinking the same

    • @OvishaSanyal
      @OvishaSanyal 3 หลายเดือนก่อน

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

  • @sksahifali8769
    @sksahifali8769 ปีที่แล้ว +4

    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

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

  • @vishalkaushal4311
    @vishalkaushal4311 5 ปีที่แล้ว +2

    The best explanation one can get on you tube

  • @gayatrishinde5299
    @gayatrishinde5299 5 ปีที่แล้ว

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

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

  • @lawrencemuthui
    @lawrencemuthui 10 หลายเดือนก่อน

    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

  • @G1Joshi
    @G1Joshi 4 ปีที่แล้ว +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...

  • @rishabhverma5848
    @rishabhverma5848 3 ปีที่แล้ว +6

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

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

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

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

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

  • @TigerChess2
    @TigerChess2 3 ปีที่แล้ว +3

    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.

  • @ramchinthakayala2092
    @ramchinthakayala2092 4 ปีที่แล้ว +5

    I did pl/SQL program with this logic

  • @hmnems8202
    @hmnems8202 2 ปีที่แล้ว

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

  • @user-tp8ht4xv6f
    @user-tp8ht4xv6f ปีที่แล้ว

    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

  • @vickykumarshaw5916
    @vickykumarshaw5916 2 ปีที่แล้ว +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 2 ปีที่แล้ว +1

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

  • @BIG_BULL_22
    @BIG_BULL_22 ปีที่แล้ว

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

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

  • @mayankjadhav4509
    @mayankjadhav4509 5 ปีที่แล้ว

    Thank you sir😊

  • @yellisettimadhuri5966
    @yellisettimadhuri5966 3 ปีที่แล้ว

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

  • @nihalchavhan5
    @nihalchavhan5 2 ปีที่แล้ว +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

  • @Ahmed_JS
    @Ahmed_JS 2 ปีที่แล้ว

    Thank you bro you helped me so much

  • @adarshagrawal6855
    @adarshagrawal6855 3 ปีที่แล้ว

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

  • @AakashYadav-ch9un
    @AakashYadav-ch9un 3 ปีที่แล้ว +2

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

  • @vijaya7170
    @vijaya7170 4 ปีที่แล้ว +3

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

  • @vrishabshetty1325
    @vrishabshetty1325 4 ปีที่แล้ว

    U can add break in if func

  • @VikashKumar-kb8jh
    @VikashKumar-kb8jh 4 ปีที่แล้ว +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 3 ปีที่แล้ว

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

    • @Buddharta
      @Buddharta 3 ปีที่แล้ว +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 3 ปีที่แล้ว

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

  • @sumitsinghsolanki3749
    @sumitsinghsolanki3749 5 ปีที่แล้ว

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

  • @elinoro
    @elinoro 4 ปีที่แล้ว +2

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

    • @sudhirammeena8068
      @sudhirammeena8068 3 ปีที่แล้ว +5

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

  • @anjalibisht3374
    @anjalibisht3374 4 ปีที่แล้ว +1

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

  • @su_kumar
    @su_kumar 2 ปีที่แล้ว

    Superb sir😃

  • @true4189
    @true4189 3 ปีที่แล้ว

    Thank u

  • @user-mq8hd8nw3p
    @user-mq8hd8nw3p 5 หลายเดือนก่อน

    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

  • @102ayushkumar3
    @102ayushkumar3 3 ปีที่แล้ว

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

  • @soumeshk
    @soumeshk 2 ปีที่แล้ว

    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

  • @dineshkannan564
    @dineshkannan564 4 ปีที่แล้ว +3

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

  • @paulofeitosa4342
    @paulofeitosa4342 ปีที่แล้ว

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

    • @lawrencemuthui
      @lawrencemuthui 10 หลายเดือนก่อน

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

  • @nikeshgupta1241
    @nikeshgupta1241 2 ปีที่แล้ว

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

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

  • @vinayaksp6478
    @vinayaksp6478 3 ปีที่แล้ว +1

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

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

  • @kasiiragavarapu673
    @kasiiragavarapu673 2 ปีที่แล้ว

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

  • @antarbanik098
    @antarbanik098 ปีที่แล้ว +1

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

  • @techno-track318
    @techno-track318 ปีที่แล้ว

    Why we check 2 & 3 in part 3

  • @sumansaha8042
    @sumansaha8042 5 ปีที่แล้ว +2

    please do upload java tutorials video.

    • @rsingh6216
      @rsingh6216 3 ปีที่แล้ว

      Have you got java tutorials

  • @MariaPaula-fc3mh
    @MariaPaula-fc3mh 3 ปีที่แล้ว

    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 10 หลายเดือนก่อน

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

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

  • @aarulive3213
    @aarulive3213 2 ปีที่แล้ว

    How to find largest 9 digit prime number in c program

  • @YoYo-nt7yf
    @YoYo-nt7yf 3 ปีที่แล้ว

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

  • @paulofeitosa4342
    @paulofeitosa4342 ปีที่แล้ว

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

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

  • @REFIREGAMING
    @REFIREGAMING 3 ปีที่แล้ว +1

    4 shows up as a prime number

    • @Name-is2bp
      @Name-is2bp 2 ปีที่แล้ว

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

  • @arunkumarr2302
    @arunkumarr2302 4 ปีที่แล้ว

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

  • @ekta7872
    @ekta7872 ปีที่แล้ว

    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

  • @funtimes2836
    @funtimes2836 11 หลายเดือนก่อน

    This code says that 0 is a prime no.

  • @mohamedesmael4547
    @mohamedesmael4547 ปีที่แล้ว

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

  • @wassupkashmir4750
    @wassupkashmir4750 5 ปีที่แล้ว +1

    Ist Viewer 😍

  • @ayushvats1808
    @ayushvats1808 4 ปีที่แล้ว

    👍👍

  • @YasirAli-pk8de
    @YasirAli-pk8de 4 ปีที่แล้ว +5

    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

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

  • @reflexop1566
    @reflexop1566 3 หลายเดือนก่อน

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

  • @subhranil_mukherjee
    @subhranil_mukherjee 5 ปีที่แล้ว +2

    pretty bad and inefficient way of checking

    • @subhranil_mukherjee
      @subhranil_mukherjee 5 ปีที่แล้ว +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 5 ปีที่แล้ว

      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 5 ปีที่แล้ว +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 4 ปีที่แล้ว +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.

  • @zoreladreanrivera9822
    @zoreladreanrivera9822 2 หลายเดือนก่อน

    #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

  • @rudranshgupta2429
    @rudranshgupta2429 ปีที่แล้ว

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

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

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

  • @abdelrhmnallam6416
    @abdelrhmnallam6416 10 หลายเดือนก่อน

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

  • @mathumathusan154
    @mathumathusan154 2 ปีที่แล้ว

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

  • @dy2moraw654
    @dy2moraw654 2 ปีที่แล้ว

    #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 3 ปีที่แล้ว

    Thank u