Write algorithm to find out the sum of first 10 even numbers
Answers
Answer:
Explanation:
// C++ implementation to find sum of
// first n even numbers
#include <bits/stdc++.h>
using namespace std;
// function to find sum of
// first n even numbers
int evenSum(int n)
{
int curr = 2, sum = 0;
// sum of first n even numbers
for (int i = 1; i <= n; i++) {
sum += curr;
// next even number
curr += 2;
}
// required sum
return sum;
}
// Driver program to test above
int main()
{
int n = 10;
cout << "Sum of first " << n
<< " Even numbers is: " << evenSum(n);
return 0;
}
Sum of first n terms of an A.P.(Arithmetic Progression)
= (n/2) * [2*a + (n-1)*d].....(i)
where, a is the first term of the series and d is
the difference between the adjacent terms of the series.
Here, a = 2, d = 2, applying these values to eq.(i), we get
Sum = (n/2) * [2*2 + (n-1)*2]
= (n/2) * [4 + 2*n - 2]
= (n/2) * (2*n + 2)
= n * (n + 1)