Write a C Program to Find the Sum of The Digits of a Number
/*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