Let’s see the C++ program of Sum of n number of odd natural numbers by using the inline function.
The inline function Inline T4Tutorials_sum::odd()
 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_sum::odd()
.
Making inline means that compiler can replace the function definitions of inline T4Tutorials_sum::odd()
with the place where this function is called U.odd();
.
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 |
#include<iostream> using namespace std; class T4Tutorials_sum { private: int n; int s=0; public : int odd(); }; inline T4Tutorials_sum::odd() { cout<<"Please enter the value to print the Odd Number."<<endl; cin>>n; cout<<"The output is"<<endl; for(int i=1;i<=n;i++) { cout<<2*i-1<<" "; s=s+(2*i-1); cout<<endl; } { cout<<"The sum of Odd Numbers is. "<<s; } } int main() { T4Tutorials_sum u; u.odd(); } |
Output
Please enter the value to print the Odd Number.
4
The output is
1
3
5
7
The sum of Odd Numbers is.16