Sum of Digits of an Integer Using recursion

Problem Statement
write a recursive c program to calculating sum of digits of a number
#include<stdio.h>
int sumofdigit(int n);
int main()
{
  int n,y;
  printf("enter the n \n");
  scanf("%d",&n);
  y=sumofdigit(n);
  printf("the sum is %d ",y);
  return 0;
}
int sumofdigit(int n)
{
  if(n>9)
    {
      return sumofdigit(n/10)+n%10;
    }
  else
    {
      return n;
    }
}

No comments:

Post a Comment