| | |
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 15 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 14 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 |
* adobe ansi api array arrays binarysearch calculate centimeter char character cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. lowest match matrix microsoft mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi





