# Write a c++ program to find out the sum of an A.P. series by using the inline function.

Write a c++ program to find out the sum of an A.P. series by using the inline function.

The inline function `inline T4Tutorials_AP_Series::T4Tutorials_AP_Series_Function()`

helps to increase the execution time of a program. The programmer can make a request to the compiler to make the inline function as `inline T4Tutorials_AP_Series::T4Tutorials_AP_Series_Function()`

.

Making inline means that compiler can replace the function definitions of `inline T4Tutorials_AP_Series_Function()`

with the place where this function is called `object.T4Tutorials_AP_Series_Function();`

.

The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.

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 31 32 33 34 |
#include<iostream> using namespace std; class T4Tutorials_AP_Series { protected : int n1, T4Tutorials_Difference, n2, i, ln, s1; public : T4Tutorials_AP_Series_Function(); }; inline T4Tutorials_AP_Series::T4Tutorials_AP_Series_Function() { s1 = 0; cout << "Input the starting number of the A.P. series: "<<endl; cin >> n1; cout << "Input the number of items for the A.P. series: "<<endl; cin >> n2; cout << "Input the common difference of A.P. series: "<<endl; cin >> T4Tutorials_Difference; s1 = (n2 * (2 * n1 + (n2 - 1) * T4Tutorials_Difference)) / 2; ln = n1 + (n2 - 1) * T4Tutorials_Difference; cout << "The Sum of the A.P. series are : "<<endl; for (i = n1; i <= ln; i = i + T4Tutorials_Difference) { if (i != ln) cout << i << " + "; else cout << i << " = " << s1 << endl; } } int main() { T4Tutorials_AP_Series object; object.T4Tutorials_AP_Series_Function(); } |