WAP to print the total of 1*1 – 2*2 + 3*3 – … + (-1)^(n+1)n*n

Write a program to print the total of 1*1 – 2*2 + 3*3 – … + (-1)^(n+1)n*n using recursive function

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

int series(int n) {
	if(n==1)
		return 1;
	else {
		return pow(-1,n+1)*n*n + series(n-1);
	}
}

int main() {
	printf("%d",series(5));
	return 0;
}

Output

15

Behind the scene

12 – 22 + 32 – 42 + 52 = 15

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments