#include<stdio.h>
int main()
{
int n,sum = 0;
printf("Enter the Value of n : "); /* if n = 126*/
scanf("%d",&n);
while(n>0)
{
sum = (sum + (n%10));
n= n/10;
}
printf("The sum of the digits are : %d", sum);
return 0;
}
/* Calculations
126%10 = 6;
sum = 0+6 = 6;
n= 126/10 = 12 ;
again 12%10 = 2;
sum = 6 + 2 = 8;
n= 12/10 = 1;
again sum = 8+1 = 9 */
No comments:
Post a Comment