A C++ Quick Snip Triangle loop

Post your C++ code snippets in here.
6 posts Page 1 of 1
Contributors
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

A C++ Quick Snip Triangle loop
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
        }
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

Try this out in dev c++ which can be downloaded in bloodshed.net :D
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

I will be posting more quick snips that come out of my mind hehe
User avatar
Agust1337
Coding God
Coding God
Posts: 2456
Joined: Fri Feb 19, 2010 8:18 pm

Hello Lawrence12,
Please use the edit button, if you have something on your mind & if there has no one posted yet.

Thanks.
You're crazy!
I'm not crazy, my mother had me tested. ~Sheldon Cooper
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

oh sorry about that I'll edit it.
User avatar
lawrence12
Dedicated Member
Dedicated Member
Posts: 73
Joined: Tue Mar 02, 2010 3:21 am

lawrence12 wrote:
oh sorry about that I'll edit it.
Well I cant remove my posts :(
6 posts Page 1 of 1
Return to “Quick Snips”