Cpp Program to generate number Tree

C++ program to generate number tree
Number tree program output.

C++ program to generate number tree is one of the best program to explain nesting of for loop and how they are executed.  The other such high thinking program that may appear in Inter school computer programming competitions are also available here.

The source of number tree written in C++ is as follows

/*	program to print the number tree using for loop
	made by		: rakesh kumar
*/
#include
#include
using namespace std;
int main(){
	int i,j,k,m,n;
	cout<<"\n Enter any No : "; cin>>n;
	for(i=1;i<=n;i++)
	  {
	  	if(i%2==0)
	  	   m=n-1;
	  	else
	  	   m=n-i;
		for(j=1;j<=m;j++)	
			cout<<setw(3)<<" ";
		for(k=1;k<=i;k++)
			cout<<setw(3)<<k;
		cout<<endl;
	  }
	return 0;
}

The output of the above program is as follows. This program was developed in Windows 10 using Dev C++ IDE.

%d bloggers like this: