Source code:
//Write a C program to accept an integer & find the sum of its digits
#include <stdio.h>
int main()
{
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
int digit, temp;
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Sum of the digits %d = %d", temp, sum);
return 0;
}
Output:
No comments:
Post a Comment