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 are the types of type qualifiers in c?
what is a constant pointer in C
application areas a 'c' a) operating system b) graphics, interpreter, assembler c) program evalution, communication softwares d) all the above
Explain how can I open a file so that other programs can update it at the same time?
What is static function in c?
State two uses of pointers in C?
what is the difference between north western polytechnique university and your applied colleges?? please give ur answers for this. :)
What are multibyte characters?
What does sizeof return c?
how do you programme Carrier Sense Multiple Access
What are linked lists in c?
What is operator precedence?
Define the scope of static variables.
Are the outer parentheses in return statements really optional?
Program to find the sum of digits of a given number until the sum becomes a single digit. (e.g. 12345=>1+2+3+4+5=15=>1+5=6)