C Program to Add Digits in Number using For Loop

Write a Program to Add Digits in Number using For Loop

// C Program to Add Digits in Number using For Loop

#include <stdio.h>

int main()
{
	long int n, m;
	int sum = 0;

	printf("Enter the number :--> ");
	scanf("%ld", &n);

	m = n;
        for(; n > 0 ; )
	{
		int remainder = n % 10;
		sum = sum + remainder;  //sum of digits
		n = n / 10;
	}
	printf("Sum of the digits of number %ld : %d",m, sum);
	return 0;
}

Output:

Enter the number :--> 654321
Sum of the digits of number 654321 : 21

≪ Previous | Next ≫