1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

// 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)
Program:

#include<stdio.h>
int main()
{
int n,i,j,sum=0;
printf("Enter n: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
sum=sum+j;
}
}
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(...