C Program to print the sum of digits of any number
#include<stdio.h>intmain(void)
{
int n,sum=0,rem;
printf("Enter a number : ");
scanf("%d",&n);
while(n>0)
{
rem = n%10; /*taking last digit of n*/
sum+=rem;
n/=10; /*skipping last digit of n*/
}
printf("Sum of digits=%d\n",sum);
return0;
}
No comments:
Write comments