196k views
1 vote
5. Write a function called oddLessEven that returns the sum of the odd valued digits minus the sum of the even valued digits c

1 Answer

3 votes

Answer:

Here is the function:

int oddLessEven(int num) {

if (num <= 0)

return 0;

if (num % 2 == 1)

return num % 10 + oddLessEven(num/10);

return oddLessEven(num/10) - num % 10; }

Step-by-step explanation:

Here is the complete program:

#include <stdio.h> //to use input output functions

int oddLessEven(int num); //function prototype

int main() //start of main method

{ int number; //declares a number

printf("Enter a number: "); //prompts user to enter a number

scanf("%d", &number); //reads number from user

int result = oddLessEven(number); //calls method by passing the number and store the results produced by method to result variable

printf("sum of the odd valued digits minus the sum of the even valued digits: %d",result); } //displays the result of the sum of odd value digits minus sum of the even valued digits

int oddLessEven(int num) { //function definition

if (num <= 0) //if number is less than or equals to 0

return 0; //returns 0

if (num % 2 == 1) //if number is odd

return num % 10 + oddLessEven(num/10); //takes mode of num with 10 and calls oddLessEven method recursively with parameter num/10

return oddLessEven(num/10) - num % 10; } //returns the sum of the odd valued digits minus the sum of the even valued digits

I will explain the function with an example:

Lets suppose number is 23 so the function works as follows:

if (num <= 0) this if condition is base condition which is false because 23 is not less than or equals to 0. So program moves to the next if condition:

if (num % 2 == 1) This if condition checks if the number 23 is not fully divisible by 2. This evaluates to true because 23 % 2 = 1. This is remainder part when 23 is divided by 2. So this means the input number is an odd number. So the program moves to the body of this if statement:

return num % 10 + oddLessEven(num/10); this becomes:

return 23 % 10 + oddLessEven(23/10);

23 % 10 = 3

oddLessEven(23/10) = oddLessEven(2)

so this becomes:

3 + oddLessEven(2)

Now oddLessEven(2) method has parameter 2.

if (num <= 0) is false

if (num % 2 == 1) is false because 2%2= 0

So the program moves to statement:

return oddLessEven(num/10) - num % 10; this becomes:

return oddLessEven(2/10) - 2% 10;

oddLessEven(2/10) = oddLessEven(0)

2% 10 = 2

this becomes:

oddLessEven(0) - 2

Now in oddLessEven(0) recursive call, the method has parameter 0

if (num <= 0) is true so this statement executes:

return 0;

So this becomes:

0 - 2 = 2

This part returns 2

Now moving to the complete part:

3 + oddLessEven(2)

3 + (oddLessEven(0) - 2 )

3 + (0 - 2)

3 - 2

1

So the output of the entire program is

sum of the odd valued digits minus the sum of the even valued digits: 1

The screenshot of the program along with its output is attached.

5. Write a function called oddLessEven that returns the sum of the odd valued digits-example-1
User Ken Clark
by
5.3k points