Advertisement
Shahar_Goldenberg

Untitled

Nov 27th, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int SumOfDigits(int num)
  4. {
  5.     int sum = 0;
  6.     if (num < 0)
  7.         return -1;
  8.     while (num > 0)
  9.     {
  10.         sum += (num % 10);
  11.         num /= 10;
  12.     }
  13.     return sum;
  14. }
  15.  
  16. void main()
  17. {
  18.     printf( "\nPart E - SumOfDigits\n" );
  19.     if( SumOfDigits( -5 ) != -1 )
  20.         printf( "your SumOfDigits(-5) function is wrong (-2)\n" );
  21.  
  22.     if( SumOfDigits( 19 ) != 1 )
  23.         printf( "your SumOfDigits(19) function is wrong (-2)\n" );
  24.  
  25.     if( SumOfDigits( 654987 ) != 3 )
  26.         printf( "your SumOfDigits(654987) function is wrong (-2)\n" );
  27.  
  28.     if( SumOfDigits( 1009 ) != 1 )
  29.         printf( "your SumOfDigits(1009) function is wrong (-2)\n" );
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement