+ 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; }

3rd Oct 2017, 5:45 PM
Joe Elizalde
Joe Elizalde - avatar
4 ответов
+ 5
for (int i=0;i<=number; i++) { total=total+i; //total =0 } cout <<total;
3rd Oct 2017, 6:05 PM
Swati Tiwari
+ 1
while(number!=0) {total+=number--;}
3rd Oct 2017, 5:49 PM
Morpheus
Morpheus - avatar
+ 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 );
3rd Oct 2017, 6:01 PM
Dennis
Dennis - avatar
- 1
hmm, 😅 from now on I ll be forever rebellious, @dennis and loved the direct formula way
3rd Oct 2017, 6:04 PM
Morpheus
Morpheus - avatar