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 Жыл бұрын
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-ns5pg8vu5j9 ай бұрын
Super bro
@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..
@ritwikasanyal99393 жыл бұрын
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...
@thewolverine55913 жыл бұрын
yes,3 is no needed, thank you for the information
@OvishaSanyal5 ай бұрын
I was thinking the same
@OvishaSanyal5 ай бұрын
But can you tell me why we are calculating square root here????
@funcrazygaming64104 жыл бұрын
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
@sksahifali87692 жыл бұрын
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
@027krishnadeshmukh95 жыл бұрын
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
@sst9403 жыл бұрын
#include int main() { int a,b,i; printf("enter a no. :"); scanf("%d",&a); for(i=2;i
@karthikdurai82093 жыл бұрын
I think if((count==0 && val2!=1) || (val2==2) ||(val2==3)) in this condition val2==3 is not needed...
@SMRTELUGUEDUCATION3 жыл бұрын
👍
@OvishaSanyal5 ай бұрын
Yes not needed
@Abhishek-yf7wi2 жыл бұрын
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
@mohmeddakhla18652 жыл бұрын
--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 Жыл бұрын
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
@tanmaymittal91853 жыл бұрын
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
@rishabhverma58483 жыл бұрын
why are we checking for 3 as well , 3 should be included in the loop . plz help
@vickykumarshaw59163 жыл бұрын
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-is2bp3 жыл бұрын
yes i just asked the same question! did you find the answer?? i really want to know.
@SVNIT_AADITYA Жыл бұрын
We can also write the program in this way - Printf("Thanks Neso Academy");
@G1Joshi4 жыл бұрын
Today, I learn a new thing... "Every positive integer greater than one can be written uniquely as the product of primes."
@rsingh62163 жыл бұрын
Neso is great bro.
@uraharakisuke53052 жыл бұрын
bhai ye class 6 mein hota h
@andistheinforitbutso75132 жыл бұрын
@@uraharakisuke5305 haan, class 6 ka questions UPSC exams me b atta he.. kisi b knowledge ko chota ya bara maat samjo..
@uraharakisuke53052 жыл бұрын
@@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...
@hmnems82023 жыл бұрын
this method works too: int num, counter = 0; scanf("%d", &num); for(int i = 1; i
@TigerChess23 жыл бұрын
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-kb8jh4 жыл бұрын
Well explained Sir but I would I loved it if you could have explained the logic of taking √n
@Buddharta4 жыл бұрын
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-kb8jh4 жыл бұрын
@@Buddharta thanks buddy understood
@mdf12594 жыл бұрын
@@Buddharta Bro can you explain by giving integer examples? Why we are square root the number first?
@Buddharta4 жыл бұрын
@@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
@mdf12594 жыл бұрын
@@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!
@vishalkaushal43116 жыл бұрын
The best explanation one can get on you tube
@98_shubhamr.sharma782 жыл бұрын
Sir please explain Step 2 and Step 3 in more detail, difficult for beginners like me to grasp
@ramchinthakayala20925 жыл бұрын
I did pl/SQL program with this logic
@elinoro4 жыл бұрын
If user enters 0, the output is that it's a prime number. You must add in part 3 "..&& val2 != 0"
@sudhirammeena80683 жыл бұрын
sir firstly tell us that only enter a positive number but zero is a non positive number
@nihalchavhan52 жыл бұрын
//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
@mohamedkhawaga80672 жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
And you can set a "BREAK" when the result of (val2%i) ==0. It will jumb of the loop
@lawrencemuthui Жыл бұрын
The printf part won't be evaluated...suppose val2 = 2; and i = 2;
@dineshkannan5645 жыл бұрын
Sir I don't understand about value1 initialisation can any one explain
@vijaya71705 жыл бұрын
Thank u so much sir u are really awesome I can understand each step carefully😃
@gayatrishinde52995 жыл бұрын
Great! Best ways and easy ways. Expecting more from you.Specially for campus recruitement test,like tcs
@user-mq8hd8nw3p7 ай бұрын
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
@yellisettimadhuri59664 жыл бұрын
#include int main() { int n,i,count=0; printf("enter a number="); scanf("%d",& n); for(i=1;i
@AakashYadav-ch9un3 жыл бұрын
Why 3 is a special case I think it will be calculated
@soumeshk3 жыл бұрын
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
@safwanbinamir51743 жыл бұрын
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; }
@andistheinforitbutso75132 жыл бұрын
Wrong
@Ahmed_JS2 жыл бұрын
Thank you bro you helped me so much
@102ayushkumar33 жыл бұрын
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; }
@true41893 жыл бұрын
Thank u
@user-ys8sl8dc8lАй бұрын
easy code : #include #include int main() { int number; int count = 0; printf("Enter a Number : "); scanf("%d", &number); if (number
@YasirAli-pk8de4 жыл бұрын
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
@mayankjadhav45096 жыл бұрын
Thank you sir😊
@su_kumar3 жыл бұрын
Superb sir😃
@anjalibisht33744 жыл бұрын
Best C programming lectures, sir plzz upload video lectures of C++
@adarshagrawal68554 жыл бұрын
what is time complexity of this program? is it better than sieve of erastothenes?
@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 Жыл бұрын
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 Жыл бұрын
Why are we setting the limit to sqrt(x)?
@NobodyKnows0982 жыл бұрын
I don't understand why you write val2 = x.
@shashanksrivastava4184 жыл бұрын
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
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 Жыл бұрын
Sure, just name it "Program to print prime numbers" 🤓🤓
@vrishabshetty13255 жыл бұрын
U can add break in if func
@sumitsinghsolanki37495 жыл бұрын
Sir your explanation is very good. Thanks for teaching us.
@shreenathkv38793 жыл бұрын
Seriously this program didn't work I had to modify the code to get this but your logic was correct.
@nikeshgupta12413 жыл бұрын
main() int x,i; printf("enter a number"); scanf("%d",&x); for(i=2;i
@vinayaksp64784 жыл бұрын
He is teaching Complicated programs it's very easy compare to what he teaching
@kasiiragavarapu6732 жыл бұрын
How we can write 7 as a product of two primes?
@techno-track318 Жыл бұрын
Why we check 2 & 3 in part 3
@ekta7872 Жыл бұрын
sir when i am trying to run these functions in my compiler it is showing error plz tell me why...
@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 Жыл бұрын
The input of number 3 is covered by the loop baby...lol
@aarulive32132 жыл бұрын
How to find largest 9 digit prime number in c program
@REFIREGAMING3 жыл бұрын
4 shows up as a prime number
@Name-is2bp3 жыл бұрын
it worked fine for me, re-check your code. :)
@YoYo-nt7yf4 жыл бұрын
Your accent is very nice to the ears. I cant listen to others vid. lectures with bad accent.
@mohamedesmael45472 жыл бұрын
int n, i, c = 0; printf("Enter any number n:"); scanf("%d", &n); //logic for (i = 1; i
@funtimes2836 Жыл бұрын
This code says that 0 is a prime no.
@ayushvats18084 жыл бұрын
👍👍
@arunkumarr23024 жыл бұрын
Square root without math.h.It is possible put a video
@shreedharchavan70333 жыл бұрын
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
@wassupkashmir47506 жыл бұрын
Ist Viewer 😍
@zoreladreanrivera98224 ай бұрын
#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
@reflexop15665 ай бұрын
Chipi chipi chapa chapa dubi rubi daba daba magic poni dubi rubi bum bum buk
@subhranil_mukherjee6 жыл бұрын
pretty bad and inefficient way of checking
@subhranil_mukherjee6 жыл бұрын
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_mukherjee6 жыл бұрын
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_mukherjee6 жыл бұрын
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.
@umairalvi73825 жыл бұрын
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.
@nandanvinjamury4 жыл бұрын
@@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-nl7ve1kb2m2 жыл бұрын
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 Жыл бұрын
#include int main() { int x,count,i; printf("\tEnter a positive natural number = "); scanf("%d",&x); count = 0; for(i=1;i
@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 Жыл бұрын
#include int main() { int num = 0; scanf("%d",&num); if(num == 1) { printf("Not prime number"); return 0; } for(int i = 2; i
@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; }
@mathumathusan1542 жыл бұрын
#include int main(){ int n,i,count=0,d; printf("enter the no :"); scanf("%d",&n); for(i=1;i
@dy2moraw6542 жыл бұрын
#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; }