Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tested an input of 4, but i am getting 1, where i should be getting 5.

What I have tried:

C
#include<stdio.h>

int main() {
    int X,Y;
    scanf("%d", &X);
    Y = checkPrime(X, X);

    printf("%d", Y+1);

}


int checkPrime(int n, int j){
    if (n==1){
        return 1;
    } else {
        if (n % j == 0){
            return 0;
        } else{
            //checkPrime(n,j-1);
            printf("%d", n+1);
        }
    }
  

}
Posted
Updated 14-May-22 6:03am
v2
Comments
Rick York 14-May-22 12:30pm    
Most prime number checkers start with 2 and then test 3 and then they skip by two. This is because 4 and the rest of the even numbers have already been tested. This means only the odd numbers need to be checked after two is tested.

First off, you need to provide a function prototype before the definition of main:
int checkPrime(int n, int j);

Second, you have commented out the bit that makes it work:
checkPrime(n,j-1);

Third, why on earth is this recursive anyway? Use a loop instead - recursive methods will crash very quickly if you provide a large input as the stack is not very big...

Fourth, read the question again. Your code will not do what your teacher wants: the next largest prime greater than a given number.

Get a loop-based prime test working first, then think about the rest of the question.
 
Share this answer
 
Comments
Achyuth S.S. 14-May-22 6:04am    
Thank you , I got it. The only reason I am using recursions is becuase I am testing it only for msmall number inputs
OriginalGriff 14-May-22 6:41am    
It's still easier to use a loop! :laugh:
Quote:
I tested an input of 4, but i am getting 1, where i should be getting 5.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Achyuth S.S. 14-May-22 6:05am    
Do you recommend gdb debugger for C prog?
CPallini 14-May-22 7:01am    
5.
Patrice T 14-May-22 7:49am    
Thank you
Before start coding you should do the obvious thing and understand the theoritacal basics. For this you may read some wikipedia about prime numbers. It contains some useful information before start coding.
 
Share this answer
 
Comments
CPallini 14-May-22 7:00am    
5.
As a starting point, you need a working primality test. Your primality test is broken.
Try
C
int primality_test(int n, int j)
{
  if ( j==1)
    return 1;
  if ( n % j == 0)
    return 0;
  return primality_test(n, j-1);
}

It is not efficient nor robust, but basically working.
 
Share this answer
 

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