Tuesday, 5 September 2017

C-Program for Sum of all digits in a number



#include<stdio.h>

int main()
{
 int dummy,n,sum=0,x;
 printf("Enter a number\n");
 scanf("%d",&n);
 dummy=n;
 while(n>0)
 {
   x=n%10;
   sum=sum+x;
   n=n/10;
 }
 printf("The sum of all digits in %d is %d\n",dummy,sum);
return 0;
}

No comments:

Post a Comment