First Upload
This commit is contained in:
36
21 - recorstion/ex4.c
Normal file
36
21 - recorstion/ex4.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include "General.h"
|
||||
|
||||
//---------------------------------------------------------------------------------------
|
||||
// DiffEvenSumToOddSum
|
||||
// -------------------
|
||||
//
|
||||
// General : Checks the difference between the number of odd digits and the
|
||||
// odd number.
|
||||
//
|
||||
// Parameters :
|
||||
// num - a number (int)
|
||||
//
|
||||
// Return value : The difference between the number of odd digits and the
|
||||
// odd number. (unsigned short).
|
||||
//
|
||||
//---------------------------------------------------------------------------------------
|
||||
// Programmer : Cohen Idan
|
||||
// Student No : 211675038
|
||||
// Date : 30.12.2019
|
||||
//---------------------------------------------------------------------------------------
|
||||
short DiffEvenSumToOddSum(int num)
|
||||
{
|
||||
int digit;
|
||||
if (num < TEN)
|
||||
return ((num % TWO) ? num : -num);
|
||||
else
|
||||
{
|
||||
digit = num % TEN;
|
||||
return (DiffEvenSumToOddSum(num / TEN) + ((digit % TWO) ? digit : -digit));
|
||||
}
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
printf("%hd\n", DiffEvenSumToOddSum(12345));
|
||||
}
|
||||
Reference in New Issue
Block a user