# Prime

This is a discussion on Prime within the C Programming forums, part of the General Programming Boards category; Computing teacher gave me homework to make a program which checks if a number is prime i wrote this: Code: ...

1. ## Prime

Computing teacher gave me homework to make a program which checks if a number is prime i wrote this:
Code:
int asal(int a,int b){
if(b=1){return 2;}
else if(a%b!=0){return asal(a,b-1);}
else if(a%b==0){return 1;}}

int main(){
if(asal(27,26)==2){printf(" is a prime");}
if(asal(27,26)==1){printf(" isn't a prime");}
getchar();
return 0;}
it returns "its a prime" where is the problem????

2. Try adding a printf() to the beginning of asal() to show the value of a and b respectively.

--
Mats

3. Why does your function that checks for prime numbers require two inputs? That's just odd.

Quzah.

4. Uh, your function returns either 1 or 2, both of which mean "true". Perhaps use 0 for false.

5. Nope. The problem is just "if (b==1)"

Also I guess it need not be called twice...hmm.

6. Another suggestion would be to work off existing code that specializes in figuring out/calculating prime numbers which will be the openssl source code package. If you are fond at reviewing source code this would be a good place to seek out how efficient code is written. A great learning tool IMHO.

7. It makes more sense to just type "prime" in the search box here, than it does to sort through the source code for openssl...

Quzah.