Write a c program to find out the sum of an A.P. series.
Flowchart of the program to find out the sum of an A.P. series.
C++ program to find out the sum of an A.P. series.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; int main() { int n, d, n2, i, ln; int sum = 0; cout<<"Input the starting number : "; cin>>n; cout<<"Input the number of items : "; cin>>n2; cout<<"Input the common difference : "; cin>>d; sum =(n2 * (2 * n + (n2 - 1) * d)) / 2; ln = n + (n2 - 1) * d; cout<<"the Sum of the A.P. series are : " << endl; i = n; do { if (i != ln) { cout << i << " + "; } else { cout << i << " = " << sum << endl; } i = i + d; } while(i <= ln); } |
Output
Input the starting number: 2
Input the number of items: 4
Input the common difference: 6
the Sum of the A.P. series are :
2+8+14+20=44