C program to add digits of a number using modulus operator(%) to extract individual digits of number and adding them.
#include <stdio.h> int main() { int n, sum = 0, remainder; printf("Enter an integer :"); scanf("%d",&n); while(n != 0) { remainder = n % 10; sum = sum + remainder; n = n / 10; } printf("\nSum of digits of entered number = %d\n",sum); return 0; }
/*
Output:
Enter an integer : 12345
Sum of digits of entered number = 15
*/