Program to Print Prime Numbers from 1 to n using Recursion in C & C++ Programming Languages

 

Introduction:

Prime numbers are a fascinating mathematical concept that has captured the attention of scholars for centuries. These are the numbers that can only be divided by 1 and itself.  As the numbers get larger, it becomes more difficult to determine whether they are prime or not. However, with the help of recursion, it is possible to write a program that prints all the prime numbers from 1 to n in C and C++ programming languages. In this article, we will discuss how to print prime numbers from 1 to n using recursion in C and C++ programming languages.

Recursion:

 Recursion is a programming technique in which a function calls itself repeatedly and continuously until a certain condition is met. In the case of printing prime numbers, we can use recursion to check if a number is prime or not. If it is prime, we print it, and then call the function again with the next number. If it is not prime, we skip it and move on to the next number.

recursion
Recursion

 Recursion is a powerful and elegant tool in programming, and it is used to solve many complex problems in a simple and efficient way. Recursion is used when a problem can be broken down into smaller and simpler sub-problems, which can be solved recursively.

Printing Prime Numbers:

To print prime numbers from 1 to n using recursion, we need to write a function that checks whether a number is prime or not. If a number is prime, we print it, and if it is not, we move on to the next number. We can use the following algorithm to print prime numbers from 1 to n using recursion:

Algorithm:

Declare a function prime(n) that takes an integer n as an argument.

If n is less than 2, return false.

If n is equal to 2, return true.

If n is even, return false.

For each odd integer i between 3 and the square root of n, check whether n is divisible by i.

If n is divisible by i, return false.

If n is not divisible by any odd integer i between 3 and the square root of n, return true.

In the main function, call the prime function for each integer between 1 and n.

If the prime function returns true, print the integer.

C Programming Language:

In C programming language, we can use the following code to print prime numbers from 1 to n using recursion:

Code:

#include <stdio.h>
#include <stdbool.h>
#include <math.h>
bool is_prime(int n, int j) {
    if (n < 2) {
        return false;
    } 
else if (j> sqrt(n))
 {
        return true;
    } 
else if (n % j == 0)
 {
        return false;
    } else {
        return is_prime(n, j+ 1);
    }
}
void print_primes(int n, int j) {
    if (j > n) {
        return;
    } else if (is_prime(j, 2)) {
        printf(“%d “, j);
    }
    print_primes(n, j + 1);
}
int main() {
    int n;
    printf(“Enter the value of n: “);
    scanf(“%d”, &n);
    print_primes(n, 2);
    printf(“\n”);
    return 0;
}

Output:

output

In this program, we define two functions – “is_prime()” and “print_primes()”.

‘Is_prime() ‘is a recursive function that takes an integer n and an integer i. It checks whether n is prime or not by dividing it with all integers between 2 and the square root of n. If n is divisible by any of these integers, it returns false. If none of them divide n, it returns true.

‘Print_primes()' is also a recursive function that takes an integer n and an integer i. It prints all the prime numbers between i and n by calling is_prime() for each number between i and n. If is_prime() returns true for a number, it prints that number.

We call print_primes() with initial values i=2 and n as input by the user in the main() function.

C++ Programming Language:

In C++ programming language, we can use the following code to print prime numbers from 1 to n using recursion:

Code:

#include <iostream>
#include <cmath>
using namespace std;
bool is_prime(int n, int j) {
    if (n < 2) {
        return false;
    }
 else if (j > sqrt(n))
 {
        return true;
    } else if (n % j = = 0) {
        return false;
    } else {
        return is_prime(n, j + 1);
    }
}
void print_primes(int n, int j) {
    if (j > n) {
        return;
    } else if (is_prime(j, 2)) {
        cout << j<< “ “;
    }
    print_primes(n, j + 1);
}
int main() {
    int n;
    cout << “Enter the value of n: “;
    cin >> n;
    print_primes(n, 2);
    cout << endl;
    return 0;
}

Output:

 

output


The program in C++ is similar to the program in C, with the main differences being in the input/output statements (cout and cin) and the absence of the stdbool.h library (since C++ has a bool data type inbuilt).

We call print_primes() with initial values i=2 and n as input by the user in the main() function.

Both programs work in a similar way – they recursively check whether each number between 2 and n is prime or not, and print the prime numbers. These programs are efficient for small values of n, but for larger values of n, they can become slow due to the recursive function calls.


Conclusion:

In conclusion, the program to print prime numbers from 1 to n using recursion in C and C++ programming languages is an important tool for generating prime numbers for a given range of values. The program uses the recursive approach to check if a number is prime or not, and then prints the prime numbers in the given range. We have seen how the program works and how it can be implemented in both C and C++ programming languages. Recursion is a powerful technique that can be used to solve many programming problems, and the program we have developed is a good example of this. Overall, this article provides a useful resource for programmers who want to learn how to use recursion to generate prime numbers.



Post a Comment

0 Comments