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
Explain what are multibyte characters?
Is python a c language?
What will the preprocessor do for a program?
Difference between macros and inline functions? Can a function be forced as inline?
What are types of preprocessor in c?
what is bit rate & baud rate? plz give wave forms
Explain what would happen to x in this expression: x += 15; (assuming the value of x is 5)
How do I round numbers?
What is the package for freshers(Non IIT) in amazon(hyderabad). And what is the same for those who are a contract employee.
Explain high-order and low-order bytes.
Explain what is #line used for?
How important is structure in life?
Explain what is meant by high-order and low-order bytes?
Can a file other than a .h file be included with #include?
Can you please explain the difference between exit() and _exit() function?