DEV Community

Discussion on: Project Euler #6 - Sum Square Difference

Collapse
 
rahyhub profile image
rahy-hub

my code with C++

/*Sum square difference

Problem 6
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 3025−385=2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.*/

include

using namespace std;
int square_sum(int s);

int main()
{
int limit=100 ,sum=0 ,sumQ=0;
for(int i=1;i<=limit;i++)
{
sumQ=sumQ+(i*i);
sum =sum+i;
}

cout<<"the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum is \n ";
cout<< square_sum(sum)-sumQ ;  // without function : cout<<sum*sum-sumQ
return 0;

}

int square_sum(int s)
{
return s*s;
}
Output >> 25164150

Collapse
 
nurchikgit profile image
nurchikgit

not bad)))