Users Online
· Guests Online: 104
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C Program to Find Sum of Digits of a Number using Recursion
The following C program, using recursion, finds the sum of its digits.
Here is the source code of the C program to find an element in a linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to find Sum of Digits of a Number using Recursion
*/
#include <stdio.h>
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
Here is the source code of the C program to find an element in a linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to find Sum of Digits of a Number using Recursion
*/
#include <stdio.h>
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
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.