+ 1
finish the loop program
// Finish the following program which adds up all integers from 0 to // the user's given number inclusively using a FOR loop. The total should be // assigned to the variable 'total'. #include <iostream> using namespace std; int main() { int number; int total; cout << "Enter a positive integer to find the summation of" << " all numbers from 0 to the given number." << endl; cin >> number; // TODO - Add your code here cout << "Total : " << total << endl; return 0; }
4 Answers
+ 5
for (int i=0;i<=number; i++)
{
total=total+i; //total =0
}
cout <<total;
+ 1
while(number!=0)
{total+=number--;}
+ 1
I agree @Neetish, instead of using a for loop like was asked, lets be rebellious and not use it :) ( btw total is not initialized )
Here's my share:
total = number / 2.0 * ( number + 1 );
- 1
hmm, đ
from now on I ll be forever rebellious, @dennis and loved the direct formula way