Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int SumOfDigits(int num)
- {
- int sum = 0;
- if (num < 0)
- return -1;
- while (num > 0)
- {
- sum += (num % 10);
- num /= 10;
- }
- return sum;
- }
- void main()
- {
- printf( "\nPart E - SumOfDigits\n" );
- if( SumOfDigits( -5 ) != -1 )
- printf( "your SumOfDigits(-5) function is wrong (-2)\n" );
- if( SumOfDigits( 19 ) != 1 )
- printf( "your SumOfDigits(19) function is wrong (-2)\n" );
- if( SumOfDigits( 654987 ) != 3 )
- printf( "your SumOfDigits(654987) function is wrong (-2)\n" );
- if( SumOfDigits( 1009 ) != 1 )
- printf( "your SumOfDigits(1009) function is wrong (-2)\n" );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement