Write a program that prints the sum of digits of a number using for loop.

C Programming Language / Loop control in C Language

25738

Program:

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

Output:

 Enter the Number: 123

 Sum of digits 6

Explanation:

None

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.