0

this solution should return 1 (i.e false) if n is prime, otherwise it returns 0,

the problem is that it always returns 1 because of the first if clause (I've specified a number in the debugging properties as usual)

 if (argc != 2) {
        return 1; 
    }

#include <stdio.h> // for ptrintf function
#include <math.h> // for square root function
#include <stdlib.h> // for strtol function 

int is_prime(int n, int i) {
    
    if (i == sqrt(n)) {
        return 0; 
    }
    if (n % i == 0) {
        return 1; 
    }
    return is_prime(n, i + 1); 
    
    /*  for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return 1; 
        }
    }  
    return 0; */ 
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        return 1; 
    }
    int n = strtol(argv[1], NULL, 10); 
    
    if (n < 0) {
        return 1;
    }
    int boolean_n = is_prime(n, 2); 

    return 0; 
}
  • How are you executing the program? How many command-line arguments do you give? – Barmar Jun 12 '23 at 06:44
  • yes, I've debugged it – Gabriel Burzacchini Jun 12 '23 at 06:45
  • I didn't ask a yes/no question. – Barmar Jun 12 '23 at 06:45
  • 3
    Try printing the value of `argc`. – Barmar Jun 12 '23 at 06:46
  • @barmar, program accepts only 1 argument (2 with the program name), I have to give only one argument – Gabriel Burzacchini Jun 12 '23 at 06:46
  • I know you have to. It thinks you're not doing it properly. Edit the question and show how you run the program. – Barmar Jun 12 '23 at 06:47
  • "I have to give only one argument " - in your own words: **what exactly do you do** in order to give that argument to the program? Why do you believe that this is the correct way to give the argument? "I've specified a number in the debugging properties as usual" - **what does this mean**? What are "the debugging properties"? Please keep in mind that your IDE is **just another program**. – Karl Knechtel Jun 12 '23 at 06:49
  • @Barmar I used the project properties of visual studio, I have always done like this – Gabriel Burzacchini Jun 12 '23 at 06:49
  • "I used the project properties of visual studio" - **This tells us nothing**. We cannot see what you typed, where, and cannot tell what you expect it to mean when you do so. – Karl Knechtel Jun 12 '23 at 06:50
  • `argc` is the amount of arguments you are using to run your program. If your program is called `Do_something.exe` and you run `Do_something.exe`, then `argc` will be zero. When you run `Do_something.exe 5` then `argc` will be equal to one. when you run `Do_something.exe 5 38` `argc` will be two. – Dominique Jun 12 '23 at 06:51
  • @Dominique in visual studio we don't have to specify the program name if i specify only 1 number, argc is two because program name is specified by default, i didn't write the program name because visual sutdio already knows it – Gabriel Burzacchini Jun 12 '23 at 06:52
  • @GabrielBurzacchini: ... but you do need to specify the command line arguments: "Project", "Properties", "Debug", "Start options". When you fill nothing in there, you won't have any arguments and `argc` will always be zero. but if you fill in something in there (like `5 38`), you'll see that `argc` becomes two (or is it three, it's possible that the program name is also included in `argc`, I'm not sure). – Dominique Jun 12 '23 at 06:56
  • "return 1 (i.e false)" Ouch! `false` is not `1`. – Gerhardh Jun 12 '23 at 09:22
  • 1
    @Dominique Actually, you are one off in `argc`, in your first example `Do_something.exe` without any argument `argc` is 1 (the name of the executable), and in your second example `Do_something.exe 5` it is 2. – the busybee Jun 12 '23 at 12:55
  • @thebusybee: I know, I corrected it in a next comment. The main point is that I believe the author does not know the meaning of `argc` and does not seem to know how to use extra commandline arguments. – Dominique Jun 12 '23 at 13:32

0 Answers0