# Data Structure Questions and Answers-Sum of n Natural Numbers using Recursion

## Data Structure Questions and Answers-Sum of n Natural Numbers using Recursion

 Question 6 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Consider the following code:

`#include<stdio.h> int recursive....sum(int n) { if(n == 0) return 0; return .....; } int main() { int n = 5; int ans = recursive....sum(n); printf("%d", ans); return 0; }`

Which of the following lines is the recurrence relation for the above code?

 A (n - 1) +recursive....sum(n) B n + recursive....sum(n) C n + recursive....sum(n - 1) D (n - 1) + recursive....sum(n - 1)
Question 6 Explanation:
The recurrence relation for the above code is: n + recursive....sum(n - 1).

 Question 7 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Consider the following code:

`#include<stdio.h> int recursive....sum(int n) { if(n == 0) return 0; return n + recursive....sum(n - 1); } int main() { int n = 5; int ans = recursive....sum(n); printf("%d", ans); return 0; }`

Which of the following is the base case for the above recursive code?

 A if(n == 0) B return 0 C return n + recursive....sum(n - 1) D none of the mentioned
Question 7 Explanation:
"if(n == 0)" is the base case for the above recursive code.

 Question 8 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the time complexity of the above recursive implementation used to find the sum of the first n natural numbers?
 A O(1) B O(n) C O(n2) D O(n3)
Question 8 Explanation:
The time complexity of the above recursive implementation used to find the sum of first n natural numbers is O(n).

 Question 9 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Which of the following methods used to find the sum of first n natural numbers has the least time complexity?
 A Recursion B Iteration C Binomial coefficient D All of the mentioned
Question 9 Explanation:
Recursion and iteration take O(n) time to find the sum of first n natural numbers while binomial coefficient takes O(1) time.

 Question 10 [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
What is the output of the following code?

`#include<stdio.h> int recursive....sum(int n) { if(n == 0) return 0; return n + recursive....sum(n - 1); } int main() { int n = 5; int ans = recursive....sum(n); printf("%d", ans); return 0; }`
 A 10 B 15 C 21 D none of the mentioned
Question 10 Explanation:
The above code prints the sum of first 5 natural numbers, which is 15.

There are 10 questions to complete.