Slip 30 - A) Write a program to find sum of digits of a given input number using user defined Function

Solution:

# include<stdio.h>

/* Function to get sum of digits */
int getSum(int n)
{
   int sum = 0;
   while (n != 0)
   {
       sum = sum + n % 10;
       n = n/10;
   }
   return sum;
}

int main()
{
  int n ;
  printf("\n enter no=");
  scanf("%d",&n);
  printf(" %d ", getSum(n));
  return 0;
}

Post a Comment

0 Comments