Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

:Hi everyone, please help me show how this problem is solved. i know the answers

ID: 3547604 • Letter: #

Question

:Hi everyone, please help me show how this problem is solved. i know the answers but need to know how its worked out.

What does the following program print?

# include<stdio.h>

int sum(int number);

int main(void)

{

int x = 3;

printf("The sum of all positive integers (<= %d) is %d. ",x,sum(x));

return 0;

}

int sum(int number)

{

int tsum;

printf("test %d at the beginning ", number);

if(number <= 1)

return 1;

tsum = number + sum(number - 1);

printf("test %d at the end ", number);

return tsum;

}

Explanation / Answer


# include<stdio.h>

int sum(int number); //function prototype

int main(void)

{

int x = 3;

printf("The sum of all positive integers (<= %d) is %d. ",x,sum(x)); // calling function

return 0;

}

int sum(int number)

{

int tsum;

printf("test %d at the beginning ", number);

if(number <= 1)

return 1;

tsum = number + sum(number - 1); //recursive call

printf("test %d at the end ", number);

return tsum;

}



number =3

tsum = 3 + sum(2)

tsum = 3 + 2 + sum(1)

tsum = 3 + 2 + 1

print 1


tsum = 6

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote