Saturday, 10 December 2011

Write a program to print sum of the Digits in c

#include
#include
void main()
{
long int num,i;
int dig,sum=0;
clrscr();
printf("Enter a Number:");
scanf("%ld",&num);
for(i=num;i>0;i=i/10)
{
dig=i%10;
sum=sum+dig;
}
printf("\n The Sum of The Digits %ld is %d",num,sum);
getch();
}

OUTPUT
Enter a Number: 12
The Sum of The Digits 12 is 3

No comments:

Post a Comment