adplus-dvertising
frame-decoration

Question

Consider the following iterative solution to find the sum of first n natural numbers:
Which of the following lines completes thebelow 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: (b).sm += i

Engage with the Community - Add Your Comment

Confused About the Answer? Ask for Details Here.

Know the Explanation? Add it Here.

Q. Consider the following iterative solution to find the sum of first n natural numbers: Which of the following lines completes thebelow code?

Similar Questions

Discover Related MCQs

Q. What is the time complexity of the above iterative method used to find the sum of the first n natural numbers?

Q. What is the time complexity of the above recursive implementation used to find the sum of the first n natural numbers?

Q. Which of the following methods used to find the sum of first n natural numbers has the least time complexity?

Q. Which of the following methods can be used to find the sum of digits of a number?

Q. What can be the maximum sum of digits for a 4 digit number?

Q. What can be the minimum sum of digits for a 4 digit number?

Q. Which of the following is the binary representation of 100?

Q. What is the time complexity of the recursive implementation used to convert a decimal number to its binary equivalent?

Q. What is the space complexity of the recursive implementation used to convert a decimal number to its binary equivalent?

Q. What is the time complexity of the above iterative implementation used to find the length of a linked list?

Q. Which of the following can be the base case for the recursive implementation used to find the length of linked list?

Q. Which of the following can be the base case for the recursive implementation used to find the length of a string?

Q. What is the time complexity of the above recursive implementation used to find the length of the string?

Q. Which of the following methods can be used to find the largest and smallest element in an array?

Q. What is the time complexity of the above iterative implementation used to find the largest and smallest element in an array?

Q. What is the time complexity of the above recursive implementation used to find the largest and the smallest element in an array?

Q. Which of the following methods can be used to find the largest and smallest number in a linked list?

Q. What is the time complexity of the above iterative code used to find the smallest and largest element in a linked list?

Q. What is the time complexity of the recursive implementation used to find the largest and smallest element in a linked list?

Q. Which of the following techniques can be used to search an element in an unsorted array?