Search⌘ K
AI Features

Cursing Recursion

Explore the concept of recursion in C programming by analyzing puzzle code designed to challenge your understanding of recursive functions and their output. Learn to predict results accurately and strengthen your problem-solving skills in recursive logic.

We'll cover the following...

Puzzle code

Read carefully the code given below:

C
#include <stdio.h>
long result(long v)
{
if( v>1 )
return(v*result(v-1));
return(v);
}
int main()
{
long a,r;
printf("Enter a positive integer: ");
scanf("%ld", &a);
r = result(a);
printf("The result is %ld\n", r);
return(0);
}

Your task: Guess the output

Attempt the following test to assess your understanding.

Technical Quiz
1.

What will be the output of the following code if the input is 4?

A.
Enter a positive integer: 4
The result is 120
B.
Enter a positive integer: 4
The result is 24
C.
Enter a positive integer: 4
The result is 4
D.
Enter a positive integer: 4
The result is 1

1 / 1

Let's discuss the code and output together in the next lesson.