No video

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

  Рет қаралды 137,344

Neso Academy

Neso Academy

Күн бұрын

Пікірлер: 115
@ziko9762
@ziko9762 Жыл бұрын
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 Жыл бұрын
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 9 ай бұрын
Super bro
@Kunal-yg9fs
@Kunal-yg9fs Ай бұрын
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..
@ritwikasanyal9939
@ritwikasanyal9939 3 жыл бұрын
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 жыл бұрын
yes,3 is no needed, thank you for the information
@OvishaSanyal
@OvishaSanyal 5 ай бұрын
I was thinking the same
@OvishaSanyal
@OvishaSanyal 5 ай бұрын
But can you tell me why we are calculating square root here????
@funcrazygaming6410
@funcrazygaming6410 4 жыл бұрын
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
@sksahifali8769
@sksahifali8769 2 жыл бұрын
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
@027krishnadeshmukh9
@027krishnadeshmukh9 5 жыл бұрын
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 жыл бұрын
I think if((count==0 && val2!=1) || (val2==2) ||(val2==3)) in this condition val2==3 is not needed...
@SMRTELUGUEDUCATION
@SMRTELUGUEDUCATION 3 жыл бұрын
👍
@OvishaSanyal
@OvishaSanyal 5 ай бұрын
Yes not needed
@Abhishek-yf7wi
@Abhishek-yf7wi 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
@mohmeddakhla1865
@mohmeddakhla1865 2 жыл бұрын
--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);
@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
@tanmaymittal9185
@tanmaymittal9185 3 жыл бұрын
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
@rishabhverma5848
@rishabhverma5848 3 жыл бұрын
why are we checking for 3 as well , 3 should be included in the loop . plz help
@vickykumarshaw5916
@vickykumarshaw5916 3 жыл бұрын
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 жыл бұрын
yes i just asked the same question! did you find the answer?? i really want to know.
@SVNIT_AADITYA
@SVNIT_AADITYA Жыл бұрын
We can also write the program in this way - Printf("Thanks Neso Academy");
@G1Joshi
@G1Joshi 4 жыл бұрын
Today, I learn a new thing... "Every positive integer greater than one can be written uniquely as the product of primes."
@rsingh6216
@rsingh6216 3 жыл бұрын
Neso is great bro.
@uraharakisuke5305
@uraharakisuke5305 2 жыл бұрын
bhai ye class 6 mein hota h
@andistheinforitbutso7513
@andistheinforitbutso7513 2 жыл бұрын
@@uraharakisuke5305 haan, class 6 ka questions UPSC exams me b atta he.. kisi b knowledge ko chota ya bara maat samjo..
@uraharakisuke5305
@uraharakisuke5305 2 жыл бұрын
@@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...
@hmnems8202
@hmnems8202 3 жыл бұрын
this method works too: int num, counter = 0; scanf("%d", &num); for(int i = 1; i
@TigerChess2
@TigerChess2 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.
@VikashKumar-kb8jh
@VikashKumar-kb8jh 4 жыл бұрын
Well explained Sir but I would I loved it if you could have explained the logic of taking √n
@Buddharta
@Buddharta 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 жыл бұрын
@@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!
@vishalkaushal4311
@vishalkaushal4311 6 жыл бұрын
The best explanation one can get on you tube
@98_shubhamr.sharma78
@98_shubhamr.sharma78 2 жыл бұрын
Sir please explain Step 2 and Step 3 in more detail, difficult for beginners like me to grasp
@ramchinthakayala2092
@ramchinthakayala2092 5 жыл бұрын
I did pl/SQL program with this logic
@elinoro
@elinoro 4 жыл бұрын
If user enters 0, the output is that it's a prime number. You must add in part 3 "..&& val2 != 0"
@sudhirammeena8068
@sudhirammeena8068 3 жыл бұрын
sir firstly tell us that only enter a positive number but zero is a non positive number
@nihalchavhan5
@nihalchavhan5 2 жыл бұрын
//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
@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
@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
@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;
@dineshkannan564
@dineshkannan564 5 жыл бұрын
Sir I don't understand about value1 initialisation can any one explain
@vijaya7170
@vijaya7170 5 жыл бұрын
Thank u so much sir u are really awesome I can understand each step carefully😃
@gayatrishinde5299
@gayatrishinde5299 5 жыл бұрын
Great! Best ways and easy ways. Expecting more from you.Specially for campus recruitement test,like tcs
@user-mq8hd8nw3p
@user-mq8hd8nw3p 7 ай бұрын
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
@yellisettimadhuri5966
@yellisettimadhuri5966 4 жыл бұрын
#include int main() { int n,i,count=0; printf("enter a number="); scanf("%d",& n); for(i=1;i
@AakashYadav-ch9un
@AakashYadav-ch9un 3 жыл бұрын
Why 3 is a special case I think it will be calculated
@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
@safwanbinamir5174
@safwanbinamir5174 3 жыл бұрын
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; }
@andistheinforitbutso7513
@andistheinforitbutso7513 2 жыл бұрын
Wrong
@Ahmed_JS
@Ahmed_JS 2 жыл бұрын
Thank you bro you helped me so much
@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; }
@true4189
@true4189 3 жыл бұрын
Thank u
@user-ys8sl8dc8l
@user-ys8sl8dc8l Ай бұрын
easy code : #include #include int main() { int number; int count = 0; printf("Enter a Number : "); scanf("%d", &number); if (number
@YasirAli-pk8de
@YasirAli-pk8de 4 жыл бұрын
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
@mayankjadhav4509
@mayankjadhav4509 6 жыл бұрын
Thank you sir😊
@su_kumar
@su_kumar 3 жыл бұрын
Superb sir😃
@anjalibisht3374
@anjalibisht3374 4 жыл бұрын
Best C programming lectures, sir plzz upload video lectures of C++
@adarshagrawal6855
@adarshagrawal6855 4 жыл бұрын
what is time complexity of this program? is it better than sieve of erastothenes?
@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.
@akram5960
@akram5960 Жыл бұрын
Why are we setting the limit to sqrt(x)?
@NobodyKnows098
@NobodyKnows098 2 жыл бұрын
I don't understand why you write val2 = x.
@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 жыл бұрын
Code execution time pdh Jayega kyuki loop phir boht Baar chlega
@sumansaha8042
@sumansaha8042 6 жыл бұрын
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 Жыл бұрын
Sure, just name it "Program to print prime numbers" 🤓🤓
@vrishabshetty1325
@vrishabshetty1325 5 жыл бұрын
U can add break in if func
@sumitsinghsolanki3749
@sumitsinghsolanki3749 5 жыл бұрын
Sir your explanation is very good. Thanks for teaching us.
@shreenathkv3879
@shreenathkv3879 3 жыл бұрын
Seriously this program didn't work I had to modify the code to get this but your logic was correct.
@nikeshgupta1241
@nikeshgupta1241 3 жыл бұрын
main() int x,i; printf("enter a number"); scanf("%d",&x); for(i=2;i
@vinayaksp6478
@vinayaksp6478 4 жыл бұрын
He is teaching Complicated programs it's very easy compare to what he teaching
@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
@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
@paulofeitosa4342
@paulofeitosa4342 Жыл бұрын
The input of number 3 is covered by the loop baby...lol
@aarulive3213
@aarulive3213 2 жыл бұрын
How to find largest 9 digit prime number in c program
@REFIREGAMING
@REFIREGAMING 3 жыл бұрын
4 shows up as a prime number
@Name-is2bp
@Name-is2bp 3 жыл бұрын
it worked fine for me, re-check your code. :)
@YoYo-nt7yf
@YoYo-nt7yf 4 жыл бұрын
Your accent is very nice to the ears. I cant listen to others vid. lectures with bad accent.
@mohamedesmael4547
@mohamedesmael4547 2 жыл бұрын
int n, i, c = 0; printf("Enter any number n:"); scanf("%d", &n); //logic for (i = 1; i
@funtimes2836
@funtimes2836 Жыл бұрын
This code says that 0 is a prime no.
@ayushvats1808
@ayushvats1808 4 жыл бұрын
👍👍
@arunkumarr2302
@arunkumarr2302 4 жыл бұрын
Square root without math.h.It is possible put a video
@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
@wassupkashmir4750
@wassupkashmir4750 6 жыл бұрын
Ist Viewer 😍
@zoreladreanrivera9822
@zoreladreanrivera9822 4 ай бұрын
#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
@reflexop1566
@reflexop1566 5 ай бұрын
Chipi chipi chapa chapa dubi rubi daba daba magic poni dubi rubi bum bum buk
@subhranil_mukherjee
@subhranil_mukherjee 6 жыл бұрын
pretty bad and inefficient way of checking
@subhranil_mukherjee
@subhranil_mukherjee 6 жыл бұрын
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 жыл бұрын
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 жыл бұрын
@@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.
@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
@rudranshgupta2429
@rudranshgupta2429 Жыл бұрын
#include int main() { int x,count,i; printf("\tEnter a positive natural number = "); scanf("%d",&x); count = 0; for(i=1;i
@ABHISHEKMISHRA-yp1jn
@ABHISHEKMISHRA-yp1jn Ай бұрын
i have shortest method #include int main(){ int n; int isprime=1; printf("enter the number:"); scanf("%d",&n); for(int i=2;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
@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 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
Check If A Number Is Prime | C Programming Example
11:57
Portfolio Courses
Рет қаралды 12 М.
拉了好大一坨#斗罗大陆#唐三小舞#小丑
00:11
超凡蜘蛛
Рет қаралды 15 МЛН
나랑 아빠가 아이스크림 먹을 때
00:15
진영민yeongmin
Рет қаралды 6 МЛН
If Barbie came to life! 💝
00:37
Meow-some! Reacts
Рет қаралды 79 МЛН
Special Programs in C − Pyramid of Stars
11:06
Neso Academy
Рет қаралды 860 М.
every good programmer should know how to code this data structure (its easy)
21:08
How to write Recursive Functions
9:03
Neso Academy
Рет қаралды 421 М.
Running a Buffer Overflow Attack - Computerphile
17:30
Computerphile
Рет қаралды 2 МЛН
Learn RECURSION in 5 minutes! 😵
5:59
Bro Code
Рет қаралды 147 М.
Call By Value & Call By Reference in C
8:34
Neso Academy
Рет қаралды 1,3 МЛН
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Problem #38 Is Prime Number
12:00
Programming Advices
Рет қаралды 54 М.
拉了好大一坨#斗罗大陆#唐三小舞#小丑
00:11
超凡蜘蛛
Рет қаралды 15 МЛН