Signup/Sign In

C++ Program To Find Sum Of Series 1 + 2 + 4 + 8 + 16 + 32 + . . . . . . . . n

Here, in this tutorial, we will be seeing how to write the program for the given pattern and at the end print the resultant sum of the series formed for the input number of terms by the user.

We can have two different approaches to write the program but based on the time complexity the second method will be much better as it will take a constant amount of time even for the large input whereas the first one will become a little bit slow for higher inputs.

C++ Program For The Sum of Series

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int i,n,sum=0;
cout<<“1+2+3+……+n”;
cout<<“\nEnter the value of n:”;
cin>>n;

for(i=1;i<=n;++i)
sum+=i;
cout<<“\nSum=”<<sum;
getch();
}


Enter the value of n: 3
Sum=6

Conclusion

There can be more than these methods to solve the same problem but the second one will be better than all since it takes a constant amount of time.