Solutions

A Sum of Numbers

Exercise 1:

#include <iostream>
using namespace std;

int main()
{        
    double sum = 0;
    for(double i=1; i<=100; i++)
        sum += i;                         
    cout << "The sum of the first 100 natural numbers is " << sum 
         << endl;
}

Exercise 2:

#include <iostream>
using namespace std;

int main()
{        
    double sum = 0;
    for(double i=1; i<=100; i++)
        sum += i*i;                         
    cout << "The sum of squares of the first 100 numbers is " << sum 
         << endl;
}

Back to exercises