Click here to Skip to main content
15,314,714 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
it's a pretty easy question, but im getting errors while submitting
Write a function isPrime(n) which outputs 1 or 0 if n is prime or not, respectively. Use this function in a program that takes a starting integer (s) and ending integer (e) as input and outputs 1 every time a prime number is detected and 0 otherwise, s and e are included. Few examples are given below.

./a.out "34" "40"
0 0 0 1 0 0 0				# only 37 as prime number

./a.out "4" "9"
0 1 0 1 0 0				# only 5 and 7 as prime numbers

What I have tried:


void isPrime(int n);
int main(int argc,char*argv[])
    int s,e;
    if(argc!=3) exit(1);
    int i;
return 0;
void isPrime(int n)
    int i, flag = 0;

    for(i=2; i<=n; ++i)
        // condition for nonprime number

    if (flag==0)
Updated 30-Mar-17 13:56pm
jeron1 30-Mar-17 17:30pm
"getting errors while submitting" Could you be more specific?
Member 13095321 30-Mar-17 17:34pm
no, it's my school portal. doesn't specify the error. just reads.."testing error."

Peter_in_2780 30-Mar-17 17:32pm
What error(s) do you get? Compile time? Run time? We can't help solve your problem if you don't tell us what it is.
Member 13095321 30-Mar-17 17:35pm
no compile time errors.
Anthony Mushrow 30-Mar-17 17:56pm
Have you tried testing your isPrime function locally before submitting? Or checking the output when you submit your program (again, if your school portal tells you what the output was).
If I run your code and pass in '0' and '10' to the program the output I get is:
PIEBALDconsult 30-Mar-17 18:06pm
It appears that the portal compiles and runs the program, and the results are incorrect.
You need to test more before submitting.

Also, your teacher is unlikely to be impressed by your implementation.
Member 13095321 30-Mar-17 18:24pm
can you suggest a better implementation?
PIEBALDconsult 30-Mar-17 19:00pm
I can.
Seek thee the Sieve of Eratosthenes.
Make a Sieve from 2 to e (pre-fill 0 and 1), then index into it to get the values for s through e.

Hi Member 13095321,

There is a mistake in this line:
for(i=2; i<=n; ++i)
It should be,
for(i=2; i<n; ++i)
Also there is no need to run up to n for checking divisibility. Half of that number is fine. Say n=37, then there is no need to loop up to 36 to check the divisibility; looping up to n/2 = 18 would be fine as later numbers (19, 20,....) are not factors of 36.

There are other efficient approaches for finding primes. You can try them as well.
PIEBALDconsult 30-Mar-17 20:32pm
No need to go beyond the square root actually.
Mehedi Shams 30-Mar-17 20:41pm
Right, thanks for correction :)
but im getting errors while submitting

Which error messages ?
Assuming you have a C compiler, you should learn how to use a debugger, it is an incredible tool.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900