Sum of Digits of a Number









Code Block
#include <stdio.h>
Including standard input output header file
int main()
Main is function from where program starts execution
{
int num=0, sum=0, remainder=0;
Declaration and initializing variables
printf(" Enter a number : ");
This message will be printed in the console
scanf("%d", &num);
Input a number here
while(num>0)
{
remainder = num%10;
sum = sum + remainder;
num = num/10;
}
printf("Sum of digits in number : %d", sum);
Sum is displayed on console
}
Output Functioning
0
Number
0
0
Remainder
Sum