A C++ Quick Snip Triangle loop
Posted: Thu Sep 23, 2010 3:32 am
Code:
Code: Select all
#include <iostream>
using namespace std;
int tri(int num);
int main(){
int n;
while(1){ // a loop
if (n == 0) // if you type 0 the program ends.
break;
cout << "Enter a number: ";
cin >> n;
cout << "Function returned: " << tri(n) << endl;
}
system("pause>nul"); // notice the nul I added that because I didn't want to see the "Press any key to continue..."
return 0; // return to nothing :D
}
// Now lets tell what the tri function should do.
int tri(int n){
int i; // i variable needed for the for loop
int sum = 0; // another variable this one is needed below
for (i = 1; i <= n; i++) // the for loop!
sum = sum + i; // sum + i
return sum; // returning the sum
}