A C++ Quick Snip Triangle loop
Post your C++ code snippets in here.
6 posts
Page 1 of 1
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
}
Try this out in dev c++ which can be downloaded in bloodshed.net
I will be posting more quick snips that come out of my mind hehe
Hello Lawrence12,
Please use the edit button, if you have something on your mind & if there has no one posted yet.
Thanks.
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
I'm not crazy, my mother had me tested. ~Sheldon Cooper
lawrence12 wrote:oh sorry about that I'll edit it.Well I cant remove my posts :(
6 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023