| | |
check 1 number is a prime or not
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 28
Reputation:
Solved Threads: 0
[CODE =C]
#include "conio.h"
#include "stdio.h"
#include "math.h"
int isPrime(int n)
{
int i,isTrue=1;
for(i=2; i<(int)sqrt(n); i++)
if(n%i==0)
return 0;
else return 1;
}
int main()
{
int N;
printf("Enter n: ");
scanf("%d",&N);
if(isPrime(N))
printf("prime");
else
printf("Not prime\n");
getch();
return 0;
}
[/CODE]
here is my source code, but when i test , sometimes it's true sometimes it's false. I don't know what is my mistake
Please help me solve it
Thanks a lot!
#include "conio.h"
#include "stdio.h"
#include "math.h"
int isPrime(int n)
{
int i,isTrue=1;
for(i=2; i<(int)sqrt(n); i++)
if(n%i==0)
return 0;
else return 1;
}
int main()
{
int N;
printf("Enter n: ");
scanf("%d",&N);
if(isPrime(N))
printf("prime");
else
printf("Not prime\n");
getch();
return 0;
}
[/CODE]
here is my source code, but when i test , sometimes it's true sometimes it's false. I don't know what is my mistake
Please help me solve it
Thanks a lot!
0
#2 16 Days Ago
Your
Try this one instead:
You can also further optimize your code by:
isPrime function will return 0 for even-number and return 1 for odd-number because your loop always end at the first loop (because you always return a value at the first loop.)Try this one instead:
C Syntax (Toggle Plain Text)
int i; for(i=2; i<(int)sqrt(n); i++) { if(n%i==0) return 0; } return 1;
You can also further optimize your code by:
int i; int k = (int)sqrt(n); for(i=2; i<k; i++) { if(n%i==0) return 0; } return 1;
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Behind every smile is a tear.
Visal .In
•
•
Join Date: Apr 2009
Posts: 28
Reputation:
Solved Threads: 0
-1
#3 16 Days Ago
•
•
•
•
YourisPrimefunction will return 0 for even-number and return 1 for odd-number because your loop always end at the first loop (because you always return a value at the first loop.)
Try this one instead:
[code]
int i;
for(i=2; i<(int)sqrt(n); i++) {
if(n%i==0) return 0;
}
return 1;
[/c
You can also further optimize your code by:
int i; int k = (int)sqrt(n); for(i=2; i<k; i++) { if(n%i==0) return 0; } return 1;
I try it again but the results sometimes true sometimes wrong
![]() |
Similar Threads
- Prime number program (C++)
- PRIME number in an array (C++)
- Help on Prime Number problem!! (C++)
- Code Snippet: Check if a number is a prime number (Python) (Python)
- Program that determines if a number is prime (C++)
- Urgent: Plz Help me,Prime Number Program Problem (C++)
Other Threads in the C Forum
- Previous Thread: Problem with piping commands in C
- Next Thread: check if the element in the matrix is prime or not? if prime, print out word (pr)
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking highest histogram homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue mysql number odf owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming stack standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi





