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
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.
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..
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
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
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...
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
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
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
//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
@@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...
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
--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);
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; }
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
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
@@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
@@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!
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?
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.
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; }
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
#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
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
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?
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
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
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
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.
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 .
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.
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.
@@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.
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; }
#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; }
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
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.
Super bro
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..
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
We can also write the program in this way -
Printf("Thanks Neso Academy");
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
this method works too:
int num, counter = 0;
scanf("%d", &num);
for(int i = 1; i
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
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...
yes,3 is no needed, thank you for the information
I was thinking the same
But can you tell me why we are calculating square root here????
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
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
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
#include
int main()
{
int a,b,i;
printf("enter a no. :");
scanf("%d",&a);
for(i=2;i
I think if((count==0 && val2!=1) || (val2==2) ||(val2==3)) in this condition val2==3 is not needed...
👍
Yes not needed
//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
Today, I learn a new thing...
"Every positive integer greater than one can be written uniquely as the product of primes."
Neso is great bro.
bhai ye class 6 mein hota h
@@uraharakisuke5305 haan, class 6 ka questions UPSC exams me b atta he.. kisi b knowledge ko chota ya bara maat samjo..
@@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...
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
--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);
#include
int main()
{
int n,i,count=0;
printf("enter a number=");
scanf("%d",& n);
for(i=1;i
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;
}
Wrong
why are we checking for 3 as well , 3 should be included in the loop . plz help
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
The best explanation one can get on you tube
Well explained Sir but I would I loved it if you could have explained the logic of taking √n
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
@@Buddharta thanks buddy understood
@@Buddharta
Bro
can you explain by giving integer examples?
Why we are square root the number first?
@@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
@@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!
Thank you very much
I think
val2=x; is not necessary
Also the condition ( val2==3) is not necessary
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?
yes i just asked the same question!
did you find the answer?? i really want to know.
easy code : #include
#include
int main()
{
int number;
int count = 0;
printf("Enter a Number : ");
scanf("%d", &number);
if (number
I did pl/SQL program with this logic
Sir please explain Step 2 and Step 3 in more detail, difficult for beginners like me to grasp
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.
If user enters 0, the output is that it's a prime number. You must add in part 3 "..&& val2 != 0"
sir firstly tell us that only enter a positive number but zero is a non positive number
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;
}
And you can set a "BREAK" when the result of (val2%i) ==0. It will jumb of the loop
The printf part won't be evaluated...suppose val2 = 2; and i = 2;
Superb sir😃
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
Thank you bro you helped me so much
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
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
Thank u so much sir u are really awesome I can understand each step carefully😃
#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
main()
int x,i;
printf("enter a number");
scanf("%d",&x);
for(i=2;i
Thank you sir😊
I don't understand why you write val2 = x.
Sir I don't understand about value1 initialisation can any one explain
Great! Best ways and easy ways. Expecting more from you.Specially for campus recruitement test,like tcs
Thank u
Why 3 is a special case
I think it will be calculated
Best C programming lectures, sir plzz upload video lectures of C++
please do upload java tutorials video.
Have you got java tutorials
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
what is time complexity of this program? is it better than sieve of erastothenes?
Sir your explanation is very good.
Thanks for teaching us.
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
Code execution time pdh Jayega kyuki loop phir boht Baar chlega
He is teaching Complicated programs it's very easy compare to what he teaching
U can add break in if func
Why are we setting the limit to sqrt(x)?
Seriously this program didn't work I had to modify the code to get this but your logic was correct.
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?
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.
The input of number 3 is covered by the loop baby...lol
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
//logic
for (i = 1; i
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?
Sure, just name it "Program to print prime numbers" 🤓🤓
Your accent is very nice to the ears.
I cant listen to others vid. lectures with bad accent.
How we can write 7 as a product of two primes?
Why we check 2 & 3 in part 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
4 shows up as a prime number
it worked fine for me, re-check your code. :)
sir when i am trying to run these functions in my compiler it is showing error plz tell me why...
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
How to find largest 9 digit prime number in c program
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
👍👍
Square root without math.h.It is possible put a video
Chipi chipi chapa chapa dubi rubi daba daba magic poni dubi rubi bum bum buk
This code says that 0 is a prime no.
Ist Viewer 😍
pretty bad and inefficient way of checking
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.
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 .
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.
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.
@@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.
#include
int main()
{
int x,count,i;
printf("\tEnter a positive natural number = ");
scanf("%d",&x);
count = 0;
for(i=1;i
#include
int main() {
int num = 0;
scanf("%d",&num);
if(num == 1)
{
printf("Not prime number");
return 0;
}
for(int i = 2; i
i have shortest method
#include
int main(){
int n;
int isprime=1;
printf("enter the number:");
scanf("%d",&n);
for(int i=2;i
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;
}
#include
int main(){
int n,i,count=0,d;
printf("enter the no :");
scanf("%d",&n);
for(i=1;i
#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;
}
Thank u