Sol3hw3a
Write a program to compute the sum of the series 3 + 6 + 12 + 24 ….. such that the sum is as large as possible without exceeding 235. The program should write out how many terms are used in the sum.
#include <iostream.h>
main( )
{
|
int n=3, sum=0, term=0; |
|
while(sum<235) |
|
{ |
|
sum=sum+n; |
|
term=term+1; n=2*n; |
|
} |
|
if(sum>235) //adjusting the "term" and "sum" if the final sum |
|
{ sum=sum-n/2; //from the above while loop exceeds 235. |
|
term=term-1; |
|
} |
|
cout<<"The sum is "<<sum<<" ,using "<<term<<" terms"<<endl; cout << " enter e to exit "; char hold; cin >> hold; |
}