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 type of function is main ()?
What is structure pointer in c?
Explain what is the difference between #include and #include 'file' ?
List the difference between a While & Do While loops?
Describe explain how arrays can be passed to a user defined function
What is the argument of a function in c?
write a program for the normal snake games find in most of the mobiles.
Why is it important to memset a variable, immediately after allocating memory to it ?
Why do we use stdio h and conio h?
differentiate built-in functions and user – defined functions.
How do you list files in a directory?
What is chain pointer in c?
How is null defined in c?
Why & is used in scanf in c?
Is it possible to use curly brackets ({}) to enclose single line code in c program?