Consider the following iterative solution to find the sum of first n natural numbers:
#include<stdio.h>
int get_sum(int n)
{
int sm = 0, i;
for(i = 1; i <= n; i++)
________;
return sm;
}
int main()
{
int n = 10;
int ans = get_sum(n);
printf("%d",ans);
return 0;
}
Which of the following lines completes the above code?
#include<stdio.h>
int get_sum(int n)
{
int sm = 0, i;
for(i = 1; i <= n; i++)
________;
return sm;
}
int main()
{
int n = 10;
int ans = get_sum(n);
printf("%d",ans);
return 0;
}A. sm = i
B. sm += i
C. i = sm
D. i += sm
Answer: Option B
Related Questions on Miscellaneous on Data Structures
Which data structure is used to implement a binary heap efficiently?
A. Array
B. Linked List
C. Stack
D. Queue
In which scenario would you use a Bloom Filter?
A. For implementing a stack-based algorithm
B. To maintain a balanced binary tree
C. For efficient sorting of elements
D. To test membership in a large dataset
A. Queue
B. Stack
C. Heap
D. Array

Join The Discussion