thissosds

Can
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// overloaded function
#include <iostream>
using namespace std;



int main ()
{
  int x=1 , total=0;
   while (x !=100)
	   {total =total + x;
	   x++;
   }
   cout << "the total is "<<  total <<endl; 
   system ("PAUSE");
   }

the total is 4950
Press any key to continue . . .
Thanks does the x=1 have to be and x or can I use something else
Not sure what you had in mind.... something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
	int last = 0;

	for(int i = 0; i < 100; i++)
	{
		cout << i << " + " << last << "= " << i + last << endl;
		last = i + last;
	}

	cout << "The final number is: " << last << endl;

	return 0;
}


Or do you mean you want it to be 1 + 1 = 2, 2 + 2 = 4, and so on?
1 +2 +3 and so on
x is what ever number you want to start with.
ex. if you wanted to add all numbers together starting at 50 ( 50+51+52+53etx) you would change x to 50.
1
2
3
4
5
  int x=50 , total=0;
   while (x !=100){
	   total =total + x;
	   x++;
   }
@rtom40
I think Kapo have explained it will , Also the Code2Code wrote another way to loop
If u wanna start From 1 So u will make X =1 and wanna to sum it to 10 for example u have to change the condition
It will be like that
1
2
3
4
5
  int x= 1  , total=0;
   while (x !=10){
	   total =total + x;    // here are the statements which increase the total by x and increase x by 1 and it will happen until the condition is satisfied ( Until x becomes 10 ) // 
	   x++;
   }
Topic archived. No new replies allowed.