Calculate the Nth term - Hacker Rank Solution

Calculate the Nth term



Problem Statement


Objective

This challenge will help you learn the concept of recursion.
A function that calls itself is known as a recursive function. The C programming language supports recursion. But while using recursion, one needs to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
To prevent infinite recursion,if....else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.



Task

There is a series,S, where the next term is the sum of pervious three terms. Given the first three terms of the series, a, b, and c respectively, you have to output the nth term of the series using recursion.
Recursive method for calculating nth term is given below.
equation calculate nth term hacker rank problem
 

Input Format

  • The first line contains a single integer, n.
  • The next line contains 3 space-separated integers, a, b, and c.

Constraints

  • 1<=n<=20
  • 1<=a,b,c<=100
 

Output Format

Print the nth term of the series, S(n).



Sample Input 0

5
1 2 3

Sample Output 0

11

Explanation 0

Consider the following steps:
  1. S(1) = 1
  2. S(2) = 2
  3. S(3) = 3
  4. S(4) = S(3)+S(2)+S(1)
  5. S(5) = S(4)+S(3)+S(2)
From steps 1,2,3 and 4, we can say S(4) = 3+2+1 = 6; Then using value from step 2,3,4, and 5, we get S(5) = 6+3+2 = 11; Thus we print the 11 as our answer.



Solution :-

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//Complete the following function.

int find_nth_term(int n, int a, int b, int c) {
  //Write your code here.
  int i,arr[100];
  arr[1]=a;
  arr[2]=b;
  arr[3]=c;
  for(i=4;i<=n;i++)
  {
      arr[i]=arr[i-1]+arr[i-2]+arr[i-3];
  }
  return arr[n];
}

int main() {
    int n, a, b, c;
  
    scanf("%d %d %d %d", &n, &a, &b, &c);
    int ans = find_nth_term(n, a, b, c);
 
    printf("%d", ans); 
    return 0;
}



Disclaimer :-
the whole problem statement are given by Hackerrank.com but the solution are generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
2 Comments
  • md salman
    md salman Wednesday, December 22, 2021

    /*this by using recursion they have given recursion in the question */
    int find_nth_term(int n, int a, int b, int c)
    {
    int s1=a;
    int s2=b;
    int s3=c;
    int s4=0;
    for(int i=0;i<n;i++)
    {
    s4=find_nth_term(n-1,s1, s2, s3);


    }
    return s4;
    }

  • Unknown
    Unknown Thursday, February 10, 2022

    gotcha

Add Comment
comment url