Recursive function in c

-1

Implement the recursive function

form (n): returns the value of the sum of i * i, with i varying from 1 to n.

I've tried it here, but it's not right:

include <stdio.h>
include <stdlib.h>
int form(int n){
    if(n==1){
        return n;
   }
    return n + form(n-1);
}
int main()
{
    int n;
    scanf("%d", &n);
    printf("%d", form(n));
    return 0;
}
    
asked by anonymous 16.09.2018 / 01:27

1 answer

-1

An alternative would be to use a global variable, so the value stacked at multiplication will always be the same.

#include <stdio.h>

int x;

int form(int n){
    if(n == 1){
        return x;
    }
    return x * form(n-1);
}

int main(){
    int n;
    scanf("%d", &n);
    x = n;
    printf("%d", form(n));
    return 0;
}
    
16.09.2018 / 17:12