1 + 4 - 9 + 16 + ......................+ n²

//1 + 4 - 9 + 16 - 25 + 36 + ......................+ n²
Program:

#include<stdio.h>
int main()
{
int n,i,sum=1,sign=1;
printf("Enter n: ");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
sum=sum+(i*i)*sign;
sign=sign*(-1);
}
printf("sum= %d",sum);
return 0;
}

Output:




No comments:

Post a Comment

x^y using recursion

Program: #include<stdio.h> int power(int a,int b); int main() { int x,y,ans; printf("Enter x and y:\n "); scanf(...