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


Please Help Members By Posting Answers For Below Questions

Explain what header files do I need in order to define the standard library functions I use?

645


When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd

637


show how link list can be used to repersent the following polynomial i) 5x+2

1674


c program for searching a student details among 10 student details

1648


Write a code to generate divisors of an integer?

631






Explain the difference between structs and unions in c?

571


What is scope rule of function in c?

541


Explain about C function prototype?

600


What are the properties of union in c?

582


the real constant in c can be expressed in which of the following forms a) fractional form only b) exponential form only c) ascii form only d) both a and b

1900


Write a function that will take in a phone number and output all possible alphabetical combinations

592


What is the difference between declaring a variable by constant keyword and #define ing that variable?

2689


What does the && operator do in a program code?

690


What is the use of typedef in structure in c?

536


What is the use of function overloading in C?

671