Program to Add Digits of Number using Recursion

Program to Add Digits of Number using Recursion

  • Write a program to Add Digits of Number using Recursion in C
  • Write a program to Add Digits of Number using Recursion in C++
  • Write a program to Add Digits of Number using Recursion in Python
  • Write a program to Add Digits of Number using Recursion in PHP
  • Write a program to Add Digits of Number using Recursion in Java
  • Write a program to Add Digits of Number using Recursion in Java Script
  • Write a program to Add Digits of Number using Recursion in C#

Explanation:

Recursion allows us to repeatedly extract the last digit from a number and add it to the total of the other digits. This process keeps going until the number drops to zero, which is the base case.

Logic to Add Digits of a Number Using Recursion:

  1. Base Case:
    • If the number is 0, return 0. This is the stopping condition for the recursion.
  2. Recursive Case:
    • Extract the last digit of the number using the modulus operator (number % 10).
    • Add this digit to the result of the recursive call with the remaining digits (number // 10).
  3. The process continues until the number is reduced to 0.

Program to Add Digits of Number using Recursion

#include <stdio.h>

int sumOfDigits(int n) {
    if (n == 0) {
        return 0;
    }
    return (n % 10) + sumOfDigits(n / 10);
}

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    printf("Sum of digits = %d\n", sumOfDigits(num));

    return 0;
}

#include <iostream>
using namespace std;

int sumOfDigits(int n) {
    if (n == 0) {
        return 0;
    }
    return (n % 10) + sumOfDigits(n / 10);
}

int main() {
    int num;

    cout << "Enter a number: ";
    cin >> num;

    cout << "Sum of digits = " << sumOfDigits(num) << endl;

    return 0;
}

def sum_of_digits(n):
    if n == 0:
        return 0
    return (n % 10) + sum_of_digits(n // 10)

num = int(input("Enter a number: "))
print("Sum of digits =", sum_of_digits(num))

<?php
function sumOfDigits($n) {
    if ($n == 0) {
        return 0;
    }
    return ($n % 10) + sumOfDigits((int)($n / 10));
}

echo "Enter a number: ";
$num = intval(trim(fgets(STDIN)));

echo "Sum of digits = " . sumOfDigits($num) . "\n";
?>

import java.util.Scanner;

public class Main {
    public static int sumOfDigits(int n) {
        if (n == 0) {
            return 0;
        }
        return (n % 10) + sumOfDigits(n / 10);
    }

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

        System.out.print("Enter a number: ");
        int num = scanner.nextInt();

        System.out.println("Sum of digits = " + sumOfDigits(num));
    }
}

function sumOfDigits(n) {
    if (n === 0) {
        return 0;
    }
    return (n % 10) + sumOfDigits(Math.floor(n / 10));
}

const num = parseInt(prompt("Enter a number:"));
console.log("Sum of digits =", sumOfDigits(num));

using System;

class Program {
    static int SumOfDigits(int n) {
        if (n == 0) {
            return 0;
        }
        return (n % 10) + SumOfDigits(n / 10);
    }

    static void Main() {
        Console.Write("Enter a number: ");
        int num = int.Parse(Console.ReadLine());

        Console.WriteLine("Sum of digits = " + SumOfDigits(num));
    }
}

List of All Programs