Program to calculate sum of digits of a number in C

Source Code

// 21.ii. Program to calculate sum of digits of a number
// Arpan Das - CST 2nd Year (VOCLET)

#include <stdio.h>
//#include <conio.h>

int main() {
	int num, sum=0;
	//clrscr();

	printf("Please enter any number: ");
	scanf("%d", &num);

	while (num != 0)	{
		sum += num%10;
		num /= 10;
	}

	printf("\nSum of digits: %d", sum);

	//getch();
}

Output

Please enter any number: 153

Sum of digits: 9

Leave a Reply

Note: Your email address will not be published. Required fields are marked *