Write a program to find whether the given number is prime or
not?
Answer Posted / naman patidar
we can reduce no of iterations by changing the condition
(i<=n/2) with (i<=sqrt(n))..
This is a rule that if a number is not dividable by any no
(except 1)less than equal to the sqr root of it then the no
is prime.
int n ; // any no, user input.
int i ;
for(i=2; i<=sqrt(n); i++ )
{
if(n%i==0)
{
printf("not prime");
break;
}
}
if(i > sqrt(n)) // you can use a flag as well here
{
printf("prime no");
}
| Is This Answer Correct ? | 7 Yes | 6 No |
Post New Answer View All Answers
What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }
What is difference between union and structure in c?
Here is a good puzzle: how do you write a program which produces its own source code as output?
What is the deal on sprintf_s return value?
What is gets() function?
Explain what is wrong with this statement? Myname = ?robin?;
How is a pointer variable declared?
What is typedef struct in c?
What is omp_num_threads?
What are the advantages of c language?
how can i access hard disk address(physical address)? are we access hard disk by using far,near or huge pointer? if yes then please explain.....
What are the string functions? List some string functions available in c.
What is binary tree in c?
write a program to print the consecutive repeated character from the given string... input string is : hhhhjkutskkkkkggggj output should be like this: hhhhkkkkkgggg anyone help me...
Do you know the difference between malloc() and calloc() function?