Monday, August 4, 2014

Sum of digits of a number

Program to calculate the sum of digits of a number.

SOLUTION:


 /*
 * Sum of digits of a number
 * Written by Shaeed Khan. 
 * Date: 04 Aug 2014
 * Version- 1.0 
 * Language: C
 */

#include < stdio.h >  
#include < stdlib.h > 

void main()
{
 int x,n;
 printf("Enter a number :");
 scanf("%d",&n);
 x=sum_digit(n);
 printf("%d",x);
}

int sum_digit(int n)
{
 int s=0;
 while(n>0)
 {
  s=s + n%10;
  n=n/10;
 }
 return s;
}

No comments:

Post a Comment