Page 1 of 1

A C++ Quick Snip Triangle loop

Posted: Thu Sep 23, 2010 3:32 am
by lawrence12
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
        }

Re: A C++ Quick Snip Triangle loop

Posted: Thu Sep 23, 2010 3:33 am
by lawrence12
Try this out in dev c++ which can be downloaded in bloodshed.net :D

Re: A C++ Quick Snip Triangle loop

Posted: Thu Sep 23, 2010 3:55 am
by lawrence12
I will be posting more quick snips that come out of my mind hehe

Re: A C++ Quick Snip Triangle loop

Posted: Thu Sep 23, 2010 3:39 pm
by Agust1337
Hello Lawrence12,
Please use the edit button, if you have something on your mind & if there has no one posted yet.

Thanks.

Re: A C++ Quick Snip Triangle loop

Posted: Fri Sep 24, 2010 2:36 am
by lawrence12
oh sorry about that I'll edit it.

Re: A C++ Quick Snip Triangle loop

Posted: Fri Sep 24, 2010 2:37 am
by lawrence12
lawrence12 wrote:
oh sorry about that I'll edit it.
Well I cant remove my posts :(