C Program to Add Digits of Number using While loop

C Program to Add Digits of Number using While loop

Write C Program to Add Digits of Number using While loop

//Write a C program to add the digits in a number

#include <stdio.h>

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

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

	m = n;

	while(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