![Write a C Program to Find the Sum of The Digits of a Number Sum of The Digits, C Program, Sumit Kar, Timus Rak, while, for](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgEEuiVQujzDYGlv1PMDzDKYb5ozvCbIjo_Mt3FOu_A8h6GVex9UkCglRQEuc0wTKxbm_OgzCSMsAl0xTsTrdYwnZVmL3fFis99MAlE7B7SZTE3u-l8-Yoy14a7zhjLCMXS_y2grkLeaBz/s1600/c-programming-tutorial.jpg)
/*Using While loop*/
#include <stdio.h>
void main()
{
int n, s = 0;
printf("Enter a number: ");
scanf("%d",&n);
while (n > 0)
{
s = s + n%10;
n = n/10;
}
printf("Sum of digits is: %d", s);
}
/*Using For loop*/
#include <stdio.h>
void main()
{
int n, s;
printf("Enter a number: ");
scanf("%d",&n);
for(s=0;n>0;n=n/10)
{
s = s + n%10;
}
printf("Sum of digits is: %d", s);
}
0 Comments