C program sum of all digits in a number


#include<stdio.h>

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);

}
  
Output:-
Enter a number
673
The sum of all digits in 673 is 16

Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post