Sum of Digits of given Number using C

This Sum of Digits in the Input Number in C program providing implemenation logic to find Sum of digits in the given input number using remainder operator and a while loop.


#include<stdio.h>
void main()
{
int n, r, sum=0 , temp;
printf(“Enter one number : “);
scanf(“%d”, &n);

temp=n ;
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
n=temp;
printf(“Sum of digits of ‘%d’ is : %d\n”,n,sum);
}

Share this