triangular numbers

Apr 26, 2014 at 5:52am
this program is suppose to check if a number is triangular or not if your not familiar with triangular number they follow the pattern 1,3,6,10,15 and so on it is also suppose to tell the number of dots on each side i am probably missing somthing simple but my value keeps coming back as not triangular

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;


int main()
{
	// variables
int check,number(1),dots(1);
// loop to create -1 setinal to kill program
while (check >=0){
cout << "Enter the number to check if it is triangular or -1 to exit " << endl;
//input the number to check
cin >> check;
// start a loop to check number 
while(number<=check){
	number=number+(number+1);
	dots++;
}
// if number is equal to check then is triangular
if (number==check)
{
	cout << "Number is triangular" << endl;
	cout << "the number of dots on a side is" << dots << endl; 
}
else cout << "BAD" << endl; 
}
return 0;
}

Apr 26, 2014 at 6:40am
The while loop only finishes when number is greater than check, so number will never be equal to check after the while loop.
Apr 26, 2014 at 4:24pm
thanks worked out but now im finding i might have some kind of calculation error because when i enter a number like 6 or 10 it returns at bad or not triangular
Apr 26, 2014 at 8:11pm
x = x + (x + 1) will generate the sequence 1, 3, 7, 15, 31, 63, ...
You're looking for the sequence 1, 3, 6, 10, 15, 21, ...
Apr 26, 2014 at 8:24pm
Use
number = number * (number+1) / 2;
Apr 26, 2014 at 8:42pm
That will generate 1, 1, 1, 1, ...
Apr 26, 2014 at 9:04pm
->That will generate 1, 1, 1, 1, ...
I didn't look at the rest of his code. I assumed he had ++number somewhere. All I'm saying is that the sequence
n(n+1)/ 2 generates
1, 3, 6, 10, 15, 21
So perhaps what he wants is
number = ++number * (++number+1) / 2;

Last edited on Apr 26, 2014 at 9:06pm
Apr 26, 2014 at 9:39pm
number = ++number * (++number+1) / 2;
That expression has undefined behavior.
Apr 26, 2014 at 9:56pm
Ok, I'm forced to read the rest of his code. So then this is what he wants (using his method):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//....
		while(triangularNumber<check){
			triangularNumber = dots * (dots+1) / 2;  // The expression I mentioned
			dots++;
		}
		// if number is equal to check then is triangular
		if (triangularNumber==check)
		{
			cout << "Number is triangular" << endl;
			cout << "the number of dots on a side is " << dots-1 << endl;
		}
		else 
			cout << "BAD" << endl; 
		dots = 1;
		triangularNumber = 1;
	}
	return 0;
} 

Sample output:
Enter the number to check if it is triangular or -1 to exit
30
BAD
Enter the number to check if it is triangular or -1 to exit
21
Number is triangular
the number of dots on a side is 6
Enter the number to check if it is triangular or -1 to exit
11
BAD
Enter the number to check if it is triangular or -1 to exit
10
Number is triangular
the number of dots on a side is 4
Enter the number to check if it is triangular or -1 to exit
Last edited on Apr 26, 2014 at 10:06pm
Apr 26, 2014 at 10:03pm
Well, great job doing his work for him. He might also have some dirty laundry you can wash, too.
Apr 26, 2014 at 10:21pm
Welcome to the board!*

BTW, no need to calculate every triangular number from 1 to n. Just check to see if sqrt( 8*n + 1 ) is a whole number.

* Sorry helios. What I'm saying is I feel your pain.
Last edited on Apr 26, 2014 at 10:22pm
Topic archived. No new replies allowed.