HACKER RANK Project Euler 06 - Sum Square Difference - Solution.'C'

Project Euler Challenges-06 - Sum Square Difference Solution
Problem Statement
This problem is a programming version of Problem 6 from projecteuler.net
The sum of the squares of the first ten natural numbers is, 12+22+...+102=385. The square of the sum of the first ten natural numbers is, (1+2++10)2=552=3025. Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025385=2640.
Find the difference between the sum of the squares of the first N natural numbers and the square of the sum.
Input Format 
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
Output Format 
Print the required answer for each test case.
Constraints 
1T104 
1N104
Sample Input
2
3
10
Sample Output
22
2640

Solution:


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

int main() {
long int t;
scanf("%ld",&t);
while(t!=0)
{
long int a,i,c,e,sum=0,sum1=0;
scanf("%ld",&a);
for(i=1;i<=a;i++)
{sum=sum+pow(i,2);}
for(i=1;i<=a;i++)
{sum1=sum1+i;}
e=pow(sum1,2);
c=e-sum;
printf("%ld\n",c);
t--;
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

Thanks for Visiting, Hope this helps you....




Comments

Popular Posts