Q.no program to calculate the sum of the digits of the number using recursion in c-programming.
In this program, we will calculate the sum of the digits of the number by taking the input from the user in c-programming
recursion means function calling itself is called recursion .
#include<stdio.h>
#include<conio.h>
int sum(int n);
void main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
printf("the sum of the digits of the number is %d",sum(n));
getch();
}
int sum(int n)
{
if(n/10==0)
return(n);
else
return(n%10+sum(n/10));
}
Output:-
enter the number 343
sum of the digits of the number is 10.
No comments:
Post a Comment