C Program to Find Sum of Digits of a Number Using Recursion

Problem: Write a program to find sum of digits of a number using recursion.

C program to find sum of digits of a number using recursion

/* Aim: C Program to find Sum of Digits of a Number using Recursion */

#include<stdio.h>
    
int sum (int);

int main()
{
	int number, result;
     
	printf("Enter the number: ");
	scanf("%d", &number);

	result = sum(number);

	printf("\n Sum of digits in %d is %d\n", number, result);

	return 0;
}

int sum (int number)
{
	if (number != 0)
	{
		return (number % 10 + sum (number / 10));
	}
	else
	{
		return 0;
	}
}

/* Output of above code:-

Enter the number: 4568
Sum of digits in 4568 is 23

*/