We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<stdio.h>// Recursive function to calculate the nth termintfind_nth_term(intn,inta,intb,intc){// Base case: if n is 1, return aif(n==1){returna;}// Base case: if n is 2, return belseif(n==2){returnb;}// Base case: if n is 3, return celseif(n==3){returnc;}// Recursive case: calculate the nth term as the sum of the previous three termselse{returnfind_nth_term(n-1,b,c,a+b+c);}}intmain(){intn,a,b,c;// Read the input valuesscanf("%d",&n);scanf("%d %d %d",&a,&b,&c);// Calculate and print the nth termintresult=find_nth_term(n,a,b,c);printf("%d",result);return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Calculate the Nth term
You are viewing a single comment's thread. Return to all comments →