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
#include <iostream>
#include <cmath>
#include <cstdlib>
usingnamespace 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;
}
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
->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;
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