fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int n, sum = 0, rem;
  5. printf("Enter a number: \n");
  6. scanf("%d", &n);
  7.  
  8. while (n > 0) {
  9. rem = n % 10;
  10. sum += rem;
  11. n /= 10;
  12. }
  13.  
  14. printf("Sum of digits = %d\n", sum);
  15. return 0;
  16. }
  17.  
Success #stdin #stdout 0s 5312KB
stdin
123
stdout
Enter a number: 
Sum of digits = 6