Tuesday, July 28, 2009

C++ program to find the sum of a series?

can u give me the code to find the sum of a series in a c++ program?


eg:#include%26lt;iostream.h%26gt;....


....


...


getch();


}

C++ program to find the sum of a series?
Here's a start.





I see you are using 'getch()' from conio.h





I assume this is to insert a pause into the program?





This is not an ideal bit of coding as it is no longer supported by all compilers. Do you know that your teacher wants you to use it? (I assume this is homework?)





Here's some discussion about it if you're interested


http://forums.devarticles.com/c-c-help-5...





If you make an attempt to write some code we may be able to help you more.








# include%26lt;iostream%26gt;


using namespace std;





int main()


{





getch();





return 0;


}





NOTE: Be careful about using the answers supplied below.


merven uses 'void main()' which is not standard c++.


the other example uses some C rather than C++ in places.





And this is from smart people who know what they are talking about and are trying to help you. No disrespect of any kind intended from me towards them. Just that what they are coding may not meet your teacher's approval (depending on her/his preferences).





Are you prepared to do any work at all to help yourself and therefore help us to help you?





///
Reply:#include %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;





int main()


{


double value;


int term;





int number_of_terms = 10;


value = 0.0;





for( term=0; term%26lt;number_of_terms; term++)


{


value += 1.0 / pow(2.0, term);


}





printf( "The sum of the recipricol of the powers of 2 to %d terms is %f\n", number_of_terms, value);


return 0;


}
Reply:you need to mention sum of which series ...otherwise how can someone help you out with the code..anyway here is the code for the sum of the first 10 numbers....





#include %26lt;iostream.h%26gt;


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()





{





int i,sum;


sum=0;


clrscr();


for (i=1;i%26lt;=10;i++)


{


sum=sum+i;


}





cout%26lt;%26lt;"\n The sum of the first 10 positive integers is "%26lt;%26lt;sum%26lt;%26lt;"\n";





getch();





}

cvs

No comments:

Post a Comment