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