Calculate the Nth term | HackerRank - Samsus Salehin Sabbir

Header Ads

Calculate the Nth term | HackerRank


Problem Link : Calculate the Nth term

Solution :
      Develop by
 Samsus Salehin Sabbir 


  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. //Complete the following function.

  6. int find_nth_term(int n, int a, int b, int c) {
  7.   //Write your code here.
  8.     int first =a;
  9.     int seccond = b;
  10.     int third = c;
  11.     int i,sum =0;
  12.     for(i=4;i<=n;i++){
  13.         sum = third + seccond + first;
  14.         first = seccond;
  15.         seccond = third;
  16.         third = sum;
  17.     }
  18.     return sum;
  19. }

  20. int main() {
  21.     int n, a, b, c;
  22.   
  23.     scanf("%d %d %d %d", &n, &a, &b, &c);
  24.     int ans = find_nth_term(n, a, b, c);
  25.  
  26.     printf("%d\n", ans); 
  27.     return 0;
  28. }

No comments

Powered by Blogger.