Monday, 16 November 2015

C++ Program To Print And Find Sum Of Series 1+2+4+8+16+32+. . . N

Problem :- Write A C++ Program To Find Sum Of Given Series 1+2+4+8+16+32+.....n

Logic :- Logic is very simple you need to use one for loop and initialize with 1 and run into numbers of terms condition and increase by multiply of condition like 
i*=2 and print the value of i .
or sum=sum+i; this statement print the sum of the series .

Try Yourself  C++ Program To Find Sum Of Series 1^2+3^2+5^2+. . . . n^2

Solution :-

#include<iostream>
using namespace std;
int main()
{
//By-Ghanendra Yadav
int i,n,sum=0;
cout<<"Enter The Number Of Terms \n";
cin>>n;
cout<<"\nSeries I sGiven Below\n\n";
for(i=1;i<=n;i*=2)
{
sum+=i;
cout<<i<<" ";
}
cout<<"\n\nSum Of Above Series Is\n";
cout<<sum <<endl;;
return 0;
}

Output:-
C++ Program To Find Sum Of Series 1+2+4+8+16+32+. . . N

No comments:
Write comments

Recommended Posts × +