Sunday, March 15, 2015

(C Language) (ENG) Sum Square Difference - Project Euler Problem 6

PROBLEM

In this problem, you asked to find difference between "square of summation of first 100 natural numbers" and "summation fo squares of first 100 natural numbers".

For example from Project Euler:

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

Difference: 3025 − 385 = 2640.



This c problem is taken by me from: (https://projecteuler.net/problem=6)


SAMPLE RUN:

In addition to problem, i printed result on the screen.




SOLUTION

You can copy codes below to your compiler to execute.




#include <stdio.h>

int main(void) //main function.
{

int sumOfSq = 0; //it will hold summation of squares.
int sqOfSum = 0; //it will hold square of summation.

for (int k = 1; k <= 100; k++) //this loop will finde summation of squares.
sumOfSq = sumOfSq + k * k;
for (int k = 1; k <= 100; k++) //this loop will find summation of first 100 natural numbers.
sqOfSum = sqOfSum + k;
sqOfSum = sqOfSum * sqOfSum; //this line will find square of summation.

printf("difference between summation of squares and square of summation is %d", sqOfSum - sumOfSq); //this line will find difference and print it on the screen.

return 0;
}

No comments:

Post a Comment