idastan97

CSCI151 L16 P4

Sep 27th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int sumOfDigits(int n){
  5.     int i, d, c, s=0;
  6.     for (d=0; pow(10,d)<=n; d++);
  7.     for (i=0; i<d; i++){
  8.         c=n/pow(10,i);
  9.         c%=10;
  10.         s+=c;
  11.     }
  12.     return s;
  13. }
  14.  
  15. int main(){
  16.     int n;
  17.     scanf("%i", &n);
  18.     printf("The sum of its digits is %i", sumOfDigits(n));
  19.     return 0;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment