Home > Software engineering >  Program that prints prime numbers
Program that prints prime numbers

Time:04-05

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

CodePudding user response:

The sub-statement of this if statement that is the call of printf

 if (primeCheck(i,y)==1)
            printf("%d ", i);

gets the control when the function primeCheck returns 1.

But according to the function definition it returns 1 when y is divisible by any number in the range [i, y] and it is not necessary that this number is i..

} else if (j%i==0) {
    return 1;

In all other case the function returns 0.

As the function calls itself recursively then it returns 1 when at least for one number in the range [x, y] y is divisible by that number.

For example for your input the function always will return 1 for each number if the range [3, 5] because 10 is divisible by 5.

Actually your function does not make a sense and has nothing common with prime numbers.

Also pay attention to that to find two biggest prime numbers in a range you should start the loop from the biggest number down to the smallest number of the range.

I think you understand the assignment incorrectly. It seems you need to write non-recursive function that determines whether a give number is a prime number and write a recursive function instead of the for loop that returns two greatest prime numbers.

That is the function that recursively finds two greatest prime numbers can look something like shown in teh demonstration program below

#include <stdio.h>

struct TwoPrimes
{
    unsigned int first_prime;
    unsigned int second_prime;
};

int is_prime( unsigned int n );

struct TwoPrimes find_two_primes( unsigned int first, unsigned int last )
{
    if (first == last)
    {
        struct TwoPrimes two_primes = { ( is_prime( first ) ? last : 0 ), 0 };
        return two_primes;
    }
    else
    {
        struct TwoPrimes two_primes = find_two_primes( first   1, last );

        if (two_primes.first_prime == 0 || two_primes.second_prime == 0)
        {
            if (is_prime( first ))
            {
                if (two_primes.first_prime == 0)
                {
                    two_primes.first_prime = first;
                }
                else
                {
                    two_primes.second_prime = first;
                }
            }
        }

        return two_primes;
    }
}

int main( void )
{
    struct TwoPrimes two_primes = find_two_primes( 3, 10 );

    if (two_primes.first_prime != 0)
    {
        printf( "%u\n", two_primes.first_prime );

        if (two_primes.second_prime != 0)
        {
            printf( "%u\n", two_primes.second_prime );
        }
    }
}

What you need to do yourself is to define the function is_prime.

  • Related