Friday, March 7, 2014

WRITE A FUNCTION TO CALCULATE THE SUM OF DIGITS OF A NUMBER

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int sum(int);
int n,s;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
s=sum(n);
printf("the sum of digits of %d is %d",n,s);
getch();
}

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

No comments:

Post a Comment