Program to Add Two Numbers using Recursion

Program to Add Two Numbers using Recursion

  • Write a program to Add Two Numbers using Recursion in C
  • Write a program to Add Two Numbers using Recursion in C++
  • Write a program to Add Two Numbers using Recursion in Python
  • Write a program to Add Two Numbers using Recursion in PHP
  • Write a program to Add Two Numbers using Recursion in Java
  • Write a program to Add Two Numbers using Recursion in Java Script
  • Write a program to Add Two Numbers using Recursion in C#

Explanation:

By constantly lowering the problem until we reach the base case, we may use recursion to add two numbers. The general reasoning is as follows:

Logic to Add Two Numbers Using Recursion:

  1. Base Case: If the second number (let’s call it b) becomes 0, return the first number (a). This is the stopping condition for the recursion.
  2. Recursive Case: Add a and b by breaking down the addition step. You can reduce the second number (b) by subtracting 1, and increment the first number (a) by 1. Continue this process until b reaches 0.

For example, to add a and b, the logic would be:

  • a + b = a + (b – 1) + 1
  • Repeat the process until b becomes 0.

Program to Add Two Numbers using Recursion

#include <stdio.h>

int add(int a, int b) {
    if (b == 0) {
        return a;
    }
    return add(a + 1, b - 1);
}

int main() {
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("Sum = %d\n", add(num1, num2));

    return 0;
}

#include <iostream>
using namespace std;

int add(int a, int b) {
    if (b == 0) {
        return a;
    }
    return add(a + 1, b - 1);
}

int main() {
    int num1, num2;

    cout << "Enter two numbers: ";
    cin >> num1 >> num2;

    cout << "Sum = " << add(num1, num2) << endl;

    return 0;
}

def add(a, b):
    if b == 0:
        return a
    return add(a + 1, b - 1)

num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

print("Sum =", add(num1, num2))

<?php
function add($a, $b) {
    if ($b == 0) {
        return $a;
    }
    return add($a + 1, $b - 1);
}

echo "Enter the first number: ";
$num1 = intval(trim(fgets(STDIN)));

echo "Enter the second number: ";
$num2 = intval(trim(fgets(STDIN)));

echo "Sum = " . add($num1, $num2) . "\n";
?>

import java.util.Scanner;

public class Main {
    public static int add(int a, int b) {
        if (b == 0) {
            return a;
        }
        return add(a + 1, b - 1);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();

        System.out.print("Enter the second number: ");
        int num2 = scanner.nextInt();

        System.out.println("Sum = " + add(num1, num2));
    }
}

function add(a, b) {
    if (b === 0) {
        return a;
    }
    return add(a + 1, b - 1);
}

const num1 = parseInt(prompt("Enter the first number:"));
const num2 = parseInt(prompt("Enter the second number:"));

console.log("Sum =", add(num1, num2));

using System;

class Program {
    static int Add(int a, int b) {
        if (b == 0) {
            return a;
        }
        return Add(a + 1, b - 1);
    }

    static void Main() {
        Console.Write("Enter the first number: ");
        int num1 = int.Parse(Console.ReadLine());

        Console.Write("Enter the second number: ");
        int num2 = int.Parse(Console.ReadLine());

        Console.WriteLine("Sum = " + Add(num1, num2));
    }
}

List of All Programs