Feb 17, 2022

Euler Problem-finding difference between sum of square and square of sum problem. ( C language )

solving maths problems using C language.

 /*
The sum of the squares of the first ten natural numbers is,
 1^2 + 2^2 + 3^2 + ... 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1+2+3+4...........+10)^2 = 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 <stdio.h>


int main()
{
    int sum_upto;
    sum_upto = 100;
    int sum_of_squares = 0;
    int square_of_sum = 0;
    for (int i = 1; i <= sum_upto; i++)
    {
        sum_of_squares += (i * i);
        square_of_sum += ( i );
    }
    square_of_sum *= square_of_sum;
    int diff = square_of_sum - sum_of_squares;
    printf("Difference is : %d", diff);

    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...