Search code examples
crecursionstructprimesfunction-definition

Program that prints prime numbers


Program that prints the 2 largest prime numbers in a given range from the user using recursion. I want to get the 2 largest prime numbers in a range that is given by the user, but somehow it just prints the consecutive numbers in the range. The prime checker in my code is working when I built a program that asks the user for the end number, checks if it is a prime number and prints all prime numbers from 1 to n. But when I plugged it in this code, its not working well. I tried researching for other codes to be my reference but I cant find anything that prints the 2 largest prime numbers. It doesn't matter if the users input are prime numbers or not, the code will just check the two largest prime numbers in between. The end number or y can also be printed if its a prime number. I'm also trying to not use arrays to practice my parameter passing.

In this code, I'm trying to get the 2 largest prime number and print it. You can see the num1, and num2 are unused, they are supposed to be the variables for the 2 largest prime numbers in the given range.

#include <stdio.h>

void usersInput(int *x, int *y);
void swapping(int *x, int *y);
int primeCheck(int i, int j);

int main() {
    int x, y, i, j, num1, num2;
    
    usersInput(&x, &y);

    if (x > y) {
        swapping(&x, &y);
    }

    printf("The value of x is %d, and the value of y is %d.\n", x, y);

    printf("\nThe two largest prime numbers from %d to %d are : ", x, y);
    for (i = x; i <= y; i++)
        if (primeCheck(i, y) == 0)
            printf("%d ", i);

    return 0;
}

void usersInput(int *x, int *y) {

    printf("Enter the value of x: ");
    scanf("%d", x);
    printf("Enter the value of y: ");
    scanf("%d", y);
}

void swapping(int *x, int *y) {
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

int primeCheck(int i, int j) {
    if (j == i) {
        return 0;
    } else if (j % i == 0) {
        return 1;
    } else {
        return primeCheck(i + 1, j);
    }
}

As you can see, I don't have any functions yet for getting the 2 largest prime numbers. I don't really have idea on how to, so please help me

Here's the actual output:

Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 6 7 8 9 10

Here's the expected output

Enter the value of x: 10
Enter the value of y: 3
The value of x is 3, and the value of y is 10.
The two largest prime numbers from 3 to 10 are: 5 7

Solution

  • There are multiple problems in your code:

    • the test for primality if (primeCheck(i, y) == 0) is incorrect: you should pass 2 as the first argument and i as the second.
    • the loop should start at i = y and run while i >= x, decrementing i to find the largest primes in the range first.
    • you should store the primes and stop when 2 have been found.
    • the primality test function should be modified to return 1 if i > j to prevent infinite recursion on some inputs, such as primeCheck(2, 1).
    • testing divisors up to and including sqrt(j) is sufficient in primeCheck(). You can stop the recursion when j < i * i, which can be tested without potential overflow as j / i < i.

    Here is a modified version of your code:

    #include <stdio.h>
    
    int usersInput(int *x, int *y);
    void swapping(int *x, int *y);
    int primeCheck(int i, int j);
    
    int main() {
        int x, y, i, num1, num2;
        
        if (!usersInput(&x, &y)) {
            return 1;
        }
        if (x > y) {
            swapping(&x, &y);
        }
    
        printf("The value of x is %d, and the value of y is %d.\n", x, y);
    
        for (i = y, num1 = num2 = 0; i >= x; i--) {
            if (primeCheck(2, i) == 0) {
                if (num2 == 0) {
                    num2 = i;
                } else {
                    num1 = i;
                    break;
                }
            }
        }
        printf("The two largest prime numbers from %d to %d are: %.0d %.0d\n",
                x, y, num1, num2);
        return 0;
    }
    
    // read user input return zero on failure
    int usersInput(int *x, int *y) {
        printf("Enter the value of x: ");
        if (scanf("%d", x) != 1)
           return 0;
        printf("Enter the value of y: ");
        return scanf("%d", y) == 1;
    }
    
    void swapping(int *x, int *y) {
        int temp = *x;
        *x = *y;
        *y = temp;
    }
    
    int primeCheck(int i, int j) {
        if (j < 2) {
            return 1;  // negative, 0 or 1: not prime
        } else if (j / i < i) {
            return 0;  // all factors tested up to sqrt(j): j is prime
        } else if (j % i == 0) {
            return 1;  // composite, not prime
        } else {
            return primeCheck(i + 1, j);
        }
    }
    

    Output:

    Enter the value of x: 10
    Enter the value of y: 3
    The value of x is 3, and the value of y is 10.
    The two largest prime numbers from 3 to 10 are: 5 7