Jan 25, 2022

Hacker Rank -Calculate Nth Term (C Language)

Fibonacci _ updated -Sum of 3 previous numbers to be calculate.

 #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


int find_nth_term(int n, int a, int b, int c) {
    int term_a;
    int term_b;

    for (int i = 0; i < n-1; i++)
    {
        //1  2  3  6  11
        //a  b  c  
        //   a  b  c
        //      a  b  c
        term_a = b;
        term_b = c;
        c = a + b + c;
        b = term_b;
        a = term_a ;
        printf("%d  ", a);
       
    }
    return a;
}

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

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...