-3

Good Day,

I'm trying to make a code that would determine the prime numbers between two numbers. This should be done recursively and without any loop. So far I have this code:

#include<stdio.h>
#include<conio.h>
void prime(int x, int y){
    int waa;
    if(x <= y){
        waa = isPrime(x);
        if(waa==1 || waa == 0){
             printf("");
         }else{
             printf("%5d",waa);
         }
        prime(x+1,y);
    }
}

int isPrime(int n, int i){
    i = 2;
    if(i<n){
        if(n%i==0 && n!=2 && n!=i){
         return(0);
        }else{
            return(n);
        }
         i++;
         isPrime(n,i);      
    }
}

void main(){
    int num[2];
    clrscr();

    printf("Input 2 numbers: \n");
    scanf("%d %d", &num[0], &num[1]);
    prime(num[0], num[1]);
    getch();
}

the output between 1 and 10 is: 2,2,3,5,7,9. expected output: prime numbers 2,3,5,7

Would anyone be able to help me with this.

much appreciated

Thank you

magicianiam
  • 1,474
  • 7
  • 33
  • 70
  • 1
    Your code does not compile, in part because of this line: `waa = isPrime(x);` *(wrong number of arguments)*. Since you claim you already got output, **post code that works**. – abelenky Sep 15 '13 at 16:11
  • @abelenky sorry to disappoint you but it does compile im using Turbo C++ IDE. i don't know whats wrong with your compiler but mine works, gives a warning though but it still compiles – magicianiam Sep 15 '13 at 16:13
  • 1
    First you cited Turbo C and tagged the question `[C]`. Now you're referring to Turbo C++ (C and C++ are different). Even if your code compiles, take a look at the line `isPrime(x)` and the defined function `isPrime(int n, int i)`. Can you tell me how they're compatible? – abelenky Sep 15 '13 at 16:16
  • Turbo C++ can compile either C or C++. you can google it if you want though. – magicianiam Sep 15 '13 at 16:20
  • 1
    @magicianiam that warning is there for a reason and if you bothered to (a) read it and (b) understand what is *actually* happening the bug would come obvious. I'd also suggest using a compiler that's not at least a decade old and full of bugs. Hint: snarky replies, like your comment to abelenky, make us much less inclined to bother answering your questions and much more inclined to leave you to your own devices. – Nik Bougalis Sep 15 '13 at 18:20

1 Answers1

1

It will be simpler if function isPrime just checks whether the given integer is prime or not and outputs a Boolean value.

#include<stdio.h>
#include <math.h>

void prime(int x, int y){
    int waa;
    if ( x == 1 ) x++;
    if(x <= y){
        waa = isPrime(x,2); // second input parameter added
        if(waa != 0){
            printf("%5d",x);
         }
        prime(x+1,y);
    }
}

int isPrime(int n, int i){
    if(n%i==0 && n!=2 && n!=i){
      return(0);
    } else {
       if (i < sqrt(n)) {
            return( isPrime(n,i+1) );
        } else
         return 1;
    }
}

int main(){
    int num[2];

    printf("Input 2 numbers: \n");
    scanf("%d %d", &num[0], &num[1]);
    prime(num[0], num[1]);
    return 0;
}

Input:

1
10

Output:

2 3 5 7

http://ideone.com/gyW7ED

Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
  • what should i include for bool? – magicianiam Sep 15 '13 at 16:17
  • i get undefined symbol for bool though – magicianiam Sep 15 '13 at 16:21
  • Sorry! C does not have any built in Boolean types. Change all bools to ints and then try. I have edited my answer. – Abhishek Bansal Sep 15 '13 at 16:23
  • got it, but the problem now is that i doesn't print anything after i input the 2 numbers, i'm guessing it goes to return(0) all the time – magicianiam Sep 15 '13 at 16:27
  • 1
    I have added the full code with tested input and output. If it still does not work, then you may have to change your compiler. Note: you will have to include math.h – Abhishek Bansal Sep 15 '13 at 16:35
  • Sorry about that, it was my fault forgot to add math.h, also thank you for the help. it is greatly appreciated. :) though would you mind explaining why is the sqrt part for and what was causing my code to print non prime numbers? thank you again – magicianiam Sep 15 '13 at 16:39
  • You need to check for factors only upto sqrt(n). That is because, if there is a factor greater than sqrt(n), it will have to be multiplied by a number less than sqrt(n) to get n. You would have already checked that smaller number and found out whether it is, or is not a factor of n. – Abhishek Bansal Sep 15 '13 at 16:45
  • C does have a built-in Boolean type as of C99 called `_Bool`, and the inclusion of `` allows you to use `true`, `false` and `bool` in place of `1`, `0` and `_Bool` respectively. However, Turbo C++ AFAIK won't have any of that because of how old it is. –  Sep 15 '13 at 17:44