This C++ Program Displays First 'n' Triangle Numbers.
The Sequence of Triangle Numbers are Generated by Adding Natural Numbers.
So the 5th Triangle Number Would be 1+2+3+4+5=15.
#include <iostream>
using namespace std;
int main()
{
int i,j,n,ans=1;
cout<<"Enter value of n: ";
cin>>n;
cout<<"\nFirst "<<n<<" Triangle Numbers are:\n";
cout<<"1\n";
for(i=2;i<=n;i++)
{
ans+=i;
cout<<ans<<"\n";
}
return 0;
}
The Sequence of Triangle Numbers are Generated by Adding Natural Numbers.
So the 5th Triangle Number Would be 1+2+3+4+5=15.
#include <iostream>
using namespace std;
int main()
{
int i,j,n,ans=1;
cout<<"Enter value of n: ";
cin>>n;
cout<<"\nFirst "<<n<<" Triangle Numbers are:\n";
cout<<"1\n";
for(i=2;i<=n;i++)
{
ans+=i;
cout<<ans<<"\n";
}
return 0;
}