[ create a new paste ] login | about

Link: http://codepad.org/gGwAsR2H    [ raw code | output | fork | 2 comments ]

C, pasted on Aug 17:
#include<stdio.h>
#include<conio.h>

int main()
{
	int i, n;
	long S;
	S = 0;
	i = 1;
	printf("\nNhap n: ");
	scanf("%d", &n);

	while(i <= n)
	{
		S = S + i * i;
		i++;
	}
	printf("i = %d", i);
	printf("\nTong 1^2 + 2^2 + ... + %d^2 la: %ld", n, S);

	/*int n,i;
    int sum=0;

    printf("Enter the n i.e. max values of series: ");
    scanf("%d",&n);

    sum = (n * (n + 1) * (2 * n + 1 )) / 6;

    printf("Sum of the series : ");

    for(i =1;i<=n;i++){
         if (i != n)
             printf("%d^2 + ",i);
         else
             printf("%d^2 = %d ",i,sum);
    }*/



	getch();
	return 0;
}


Output:
1
Line 17: error: conio.h: No such file or directory


Create a new paste based on this one


Comments:
posted by 141c910036 on Oct 10
n (n+1) (2n+1)/6
reply
posted by 1461512 on Oct 25
int kq=0;
for(int i=1;i<=n;i++)
{
kq += (int)Math.Pow(i,2);
}
return kq;
reply