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