Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Calculate the Nth term solution in c

YASH PAL, 20 July 202416 January 2026

In this HackerRank Calculate the Nth term problem solution we are going to learn about the concept of recursion. in this problem, we have a series of numbers where the next term is the sum of the previous three terms. so we need to print the sum of the series on the output screen.

Calculate the nth term solution
Calculate the nth term solution

HackerRank Calculate the Nth term solution in c

#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) {
    if(n == 1)
        return a;
    else if (n == 2)
        return b;
    else if (n == 3)
        return c;
    
    return find_nth_term(n-1,a,b,c)+find_nth_term(n-2,a,b,c)+find_nth_term(n-3,a,b,c);
    
}

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;
}

Second solution

#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 tmp=0;
    if(n==1){
        return a;
    }
    else if(n==2){
        return b;
    }
    else if(n==3){
        return c;
    }
    tmp=c;
    c = find_nth_term(n-1,a,b,c);
    c += find_nth_term(n-2,a,b,c);
    c += find_nth_term(n-3,a,b,c);
    return find_nth_term(n--,c,tmp,b);

}

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;
}
C Solutions coding problems solutions Hackerrank Problems Solutions cHackerRank

Post navigation

Previous post
Next post
HackerRank C Solutions
Hello world in c solution
Playing with characters problem solution
sum and difference of two numbers problem solution
functions in c problem solution
pointers in c problem solution
Conditional statements in c problem solution
For loop in c solution
Sum of Digits of a five-digit number problem solution
Bitwise operators problem solution
Printing pattern using loops problem solution
1D Arrays in c problem solution
Array Reversal problem solution
Printing Tokens problem solution
Digit Frequency problem solution
Dynamic Array in c problem solution
Calculate the Nth term problem solution
Students Marks sum problem solution
Sorting Array of strings problem solution
Permutations of strings problem solution
Variadic functions in c problem solution
Querying the documents problem solution
Boxes through a tunnel problem solution
Small Triangles, Large Triangles problem solution
post-transition problem solution
Structuring the document problem solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes