Write a C Program to Find Out Sum of Digits of a Given Number
Write a C program to find out sum of digits of a given number.
Answers
Answered by
1
/*
* C Program to find Sum of Digits of a Number using Recursion
*/
#include
int sum (int a);
int main()
{
int num, result;
printf("Enter the number: ");
scanf("%d", &num);
result = sum(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
int sum (int num)
{
if (num != 0)
{
return (num % 10 + sum (num / 10));
}
else
{
return 0;
}
$ cc pgm25.c
$ a.out
Enter the number: 2345
Sum of digits in 2345 is 14
courtesy:sanfoundry.com
Similar questions