For C Language 1) Write a C program to calculate the factorial of a given integer using recursion

For C Language 1) Write a C program to calculate the factorial of a given integer using recursion ? 2)Implement a function in C to check if a number is prime or not ? For C++ Language 1)Write a C++ class for a basic rectangle shape with functions to calculate area and perimeter ? 2)Implement a C++ program to find the factorial of a given number using a recursive function

The Correct Answer and Explanation is :

Below are the programs and explanations for each task.


C Language Programs

1. Factorial of a number using recursion

#include <stdio.h>

int factorial(int n) {
    if (n == 0 || n == 1)
        return 1; // Base case
    return n * factorial(n - 1); // Recursive case
}

int main() {
    int num;
    printf("Enter a number to find its factorial: ");
    scanf("%d", &num);

    if (num < 0)
        printf("Factorial is not defined for negative numbers.\n");
    else
        printf("Factorial of %d is %d\n", num, factorial(num));
    return 0;
}

2. Function to check if a number is prime

#include <stdio.h>

int isPrime(int n, int i) {
    if (n <= 2)
        return n == 2; // Base case
    if (n % i == 0)
        return 0; // Not prime
    if (i * i > n)
        return 1; // Prime
    return isPrime(n, i + 1); // Recursive check
}

int main() {
    int num;
    printf("Enter a number to check if it is prime: ");
    scanf("%d", &num);

    if (num <= 1)
        printf("%d is not a prime number.\n", num);
    else if (isPrime(num, 2))
        printf("%d is a prime number.\n", num);
    else
        printf("%d is not a prime number.\n", num);

    return 0;
}

C++ Language Programs

1. Rectangle class with area and perimeter functions

#include <iostream>
using namespace std;

class Rectangle {
private:
    double length;
    double width;

public:
    Rectangle(double l, double w) : length(l), width(w) {}

    double area() {
        return length * width;
    }

    double perimeter() {
        return 2 * (length + width);
    }
};

int main() {
    double length, width;
    cout << "Enter length and width of the rectangle: ";
    cin >> length >> width;

    Rectangle rect(length, width);
    cout << "Area: " << rect.area() << endl;
    cout << "Perimeter: " << rect.perimeter() << endl;

    return 0;
}

2. Factorial using recursion in C++

#include <iostream>
using namespace std;

int factorial(int n) {
    if (n == 0 || n == 1)
        return 1; // Base case
    return n * factorial(n - 1); // Recursive case
}

int main() {
    int num;
    cout << "Enter a number to find its factorial: ";
    cin >> num;

    if (num < 0)
        cout << "Factorial is not defined for negative numbers." << endl;
    else
        cout << "Factorial of " << num << " is " << factorial(num) << endl;

    return 0;
}

Explanation (300 Words)

Factorial Programs

Both C and C++ programs use recursion to compute the factorial. The base case stops the recursion when the input is 0 or 1, as their factorial is 1. The recursive case reduces the problem by multiplying the current number with the factorial of (n-1).

Prime Number Check

The prime-checking function uses recursion in C. It starts dividing the number from 2 up to the square root of the number (i * i > n). If the number is divisible by any number in this range, it is not prime.

Rectangle Class

In the C++ class, encapsulation is used to manage length and width. Methods area and perimeter provide functionality to calculate the respective values.

Comparison

Both C and C++ programs demonstrate recursion but with differences in syntax and paradigms. The class-based approach in C++ allows modeling real-world entities (e.g., Rectangle), while C uses a more procedural style. These programs highlight recursion, object-oriented programming (OOP), and functional decomposition.

Scroll to Top