Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include<stdio.h>
main()
{
    int n, p;
    printf("Enter a number ");
    scanf("%d", &n);
    p= prime(n,n/2);
    if(p == 1)
    {
        printf("Yes");
    }
    else
    {
        printf("No");
	}
}
int prime(int n, int i)
{
    if(i == 1)
        printf("Yes");
    else
    {
        if(n%i == 0)
            printf("No");
        else
            prime(n, i-1); 
    }
}


What I have tried:

So this program is about finding prime numbers using recursion method
I have tried to run the program without the n/2 but it won't print any clarification why please
it would help me if there is any other method to get prime numbers using recursion method
Posted
Updated 28-Oct-22 4:53am
v2
Comments
jeron1 28-Oct-22 10:18am    
prime() is supposed to return an int, nothing is being return right now.
Member 15808958 28-Oct-22 10:57am    
yea but in the question they asked if it is a prime number yes should be printed if it is not a prime no should be printed using recursion
merano99 28-Oct-22 11:39am    
yes should be printed if it is NOT a prime ? With you it is currently exactly the other way around (which would also be usual). A recursion without return is also rather unusual.
jeron1 28-Oct-22 13:54pm    
True, but at some point you are counting on a return value;

p= prime(n,n/2); // you are expecting a return value here
if(p == 1) // in order to test it here
{
printf("Yes");
}

n/2 provides the starting point for the prime check.
If you think about it, n cannot be divisible by any number larger than n/2 as 2 is the smallest divisor that allows a zero remainder.
If you want to see is 11 is prime, then 11 / 2 is 5 (remember, integer division discards fractional parts) if you try to divide 11 by 6 or above, you are guaranteed not to get a zero remainder.

But ... a more efficient way to do it is to start by checking low numbers: they eliminate more non-prime values faster: 2 eliminates all the even numbers (half the possible ones)for example, then 3 eliminates one third, and so on. So check for two, then only ever check the odd values up to n/2 If it's not prime, you will get the result a lot quicker than going from the high values down.

But the big question is "Why?" - prime number calculations aren't naturally recursive - they are iterative. So an iterative solution is going to work better than a recursive one, because it will cope with larger "might be prime" values. Stacks are pretty small, and each time you recurse you use a significant amount of stack memory. It will run out, and pretty quickly!
 
Share this answer
 
The subroutine should actually have a return value and should not output anything itself, so that the main program makes sense.
Currently your code looks like this:
C
int prime(int n, int i)
{
    if(i == 1)
        printf("Yes");
    else
    {
        if(n%i == 0)
            printf("No");
        else
            prime(n, i-1); 
    }
}

Instead of the output of printf("Yes") there should be return 1. The following else could then be omitted. Accordingly, printf("No") should be replaced.

Unfortunately, your design crashes at both 0 and -1 and for other negative numbers the result is wrong.

I agree with OriginalGriff's suggestion not to start testing at n/2 and then count down, but rather to test the other way around.

If you want you can write the function isprime() with one parameter like this:
C
int isprime(int num)
{
	return prime(num, num / 2);
}
 
Share this answer
 
v3

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