Algorithms | Searching | Question 1

What is the output of following program?

#include <stdio.h>
  
void print(int n, int j)
{
   if (j >= n)
      return;
   if (n-j > 0 && n-j >= j)
       printf("%d %d\n", j, n-j);
   print(n, j+1);
}
  
int main()
{
    int n = 8;
    print(n, 1);
}

(A) 1 7
2 6
3 5
4 4
4 4

(B) 1 7
2 6
3 5
4 4
(C) 1 7
2 6
3 5
(D) 1 2
3 4
5 6
7 8


Answer: (B)

Explanation: For a given number n, the program prints all distinct pairs of positive integers with sum equal to n.

Quiz of this Question



My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :



Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.