Write a C Program to Find the Sum of The Digits of a Number
![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/AVvXsEgd7JUeFcGURR9T3358Yzicn60HdqsMSKqXUO3-Sxfx4K5Sqc7UgUwbzWzXCCtqRS4lZ_KrO_J8XbTe1fxRFzDQ-ZO_BYWAYQLuUNCKozi7hAbEpi0FGxITpJhJtEywftgs9V5CX1CA6O8a/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);
}
Comments
Post a Comment