Question #24460

Write a while loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:

21
which is 1 + 2 + 3 + 4 + 5 + 6.

For problems such as these, do not include raw_input statements or define the variable end. Our automating testing will provide a value of end for you - so write your code in the following box assuming end is already defined.
Hint: Don't Use A Variable Called 'sum'

Expert's answer

#include <iostream>

#include <conio.h>
using namespace std;


int main()
{
int number=0;
cout<<"Enter number: ";
cin>>number;
int count=0;
int sum=0;
while(count<number){
count++;
sum+=count;
}
cout<<"Sum = "<<sum;
getch();
return 0;
}

LATEST TUTORIALS
APPROVED BY CLIENTS