C++/write a do-while loop to calculate total
Expert: Prince M. Premnath - 6/30/2008
QuestionHi! Its me again. I need to write a function total and need to use a do-while loop to calculate the sum from 1 to n.
I wrote:
int total (int n)
{
n = 1;
do
{
total = n + n++;
cout << total << endl;
}
} while (n > 0);
AnswerDear Mr Ravi !
A small effort will sure bring solution to your problem ,just understanding the problem and language constructs matters
int total(int n)
{
int i = 1;
int sum = 0;
while ( i <= n )
sum+=i++;
return sum;
}
i haven't tested this code , so i kindly request you to report me in case of issues
Thanks and Regards!
Prince M. Premnath