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
why to assign a pointer to null sometimes??how can a pointer we declare get assigned with a garbage value by default???
Is return a keyword in c?
Using which language Test cases are added in .ptu file of RTRT unit testing???
Why c is faster than c++?
What is a good data structure to use for storing lines of text?
What is the purpose of ftell?
How do we declare variables in c?
What is getch () for?
hw can we delete an internal node of binary search tree the internal node has child node..plz write progarm
What is the difference between %d and %i?
Explain high-order and low-order bytes.
write a program in C that prompts the user for today's date,tomorrow's date and display the results.Use structures for today's date,tomorrow's date and an array to hold the days for each month of the year.
Which is not valid in C a) class aClass{public:int x;}; b) /* A comment */ c) char x=12;
Explain what is the benefit of using #define to declare a constant?
What is extern keyword in c?