Write a C program to find the sum of individual digits of a positive integer.

Read the number n
Initialize sum = 0
while n > 0
d = n%10
sum = sum+d
n = n/10
print sum

#include<stdio.h>
int main()
{
int n, sum=0,d;
printf("Enter any integer:");
scanf("%d", &n);
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
printf("sum of individual digits is: %d",sum);
return 0;
}
Enter any integer:1234

sum of individual digits is:10

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.