Write a program that prints the sum of digits of a number.

C Programming Language / Loop control in C Language

1042

Program:

#include <stdio.h>
int main()
{
	int n, s=0, r;
	printf("\n Enter the Number: ");
	scanf("%d", &n);
	while(n>0)
	{
		r=n%10;
		s=s+r;
		n=n/10;
	}
	printf("\nSum of digits %d", s);
	
	getch();
	return 0;
}

Output:

Output 1:

 Enter the Number: 123

Sum of digits 6

Output 2

 Enter the Number: 1111

Sum of digits 4

Explanation:

Example: Number: 123 1+2+3 = 6

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.