C++ Christmas tree

Christmas tree type output

Page 1 of 1

4 Replies - 24807 Views - Last Post: 06 January 2010 - 06:42 AM Rate Topic: -----

#1 juan_garc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-October 07

C++ Christmas tree

Posted 24 October 2008 - 04:40 PM

I am trying to make a numeric christmas tree look number. but have no success.

I need to look like this

.....1
....1 2
...1 2 3
..1 2 3 4
.1 2 3 4 5


The output from this code look like this

1
12
123
1234
12345

I am trying to use setw but the only thing that I output is the first number out the marging.

1
12
123
1234
12345

Any ideas??

This is my code so far.

#include <iostream>
#include <iomanip>

using namespace std;
const int Success = 0;

int numberss = 5; //const number to change for a dinamic arrays.
// the next part is to make the user said to how number do you want the tree.

int main()
{

   int i, j; // logical variables

   for(i = 1; i <= numberss; i++) // loop i to 5, 
   {
	  for(j = 1; j <= i; j++) // loop to count
		 cout << j << " "; // output the numbers
	  cout << endl; // next line output.
   }

	system("pause");
	return Success;

}//end of main.


*mod edit: added code tags :code:
helped with formatting for output

This post has been edited by NickDMax: 24 October 2008 - 04:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Christmas tree

#2 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: C++ Christmas tree

Posted 24 October 2008 - 04:50 PM

add: cout << setw(numberss - i + 1); to the top of the outer loop.

This will set the width for the first element of each line. The inner loop will then print the line.
Was This Post Helpful? 1
  • +
  • -

#3 saloqbi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-October 08

Re: C++ Christmas tree

Posted 25 October 2008 - 02:12 AM

wooooooow
Was This Post Helpful? 0
  • +
  • -

#4 ninad_in  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-January 10

Re: C++ Christmas tree

Posted 06 January 2010 - 06:30 AM

View Postjuan_garc, on 24 Oct, 2008 - 03:40 PM, said:

I am trying to make a numeric christmas tree look number. but have no success.

I need to look like this

.....1
....1 2
...1 2 3
..1 2 3 4
.1 2 3 4 5


The output from this code look like this

1
12
123
1234
12345

I am trying to use setw but the only thing that I output is the first number out the marging.

1
12
123
1234
12345

Any ideas??

This is my code so far.

#include <iostream>
#include <iomanip>

using namespace std;
const int Success = 0;

int numberss = 5; //const number to change for a dinamic arrays.
// the next part is to make the user said to how number do you want the tree.

int main()
{

   int i, j; // logical variables

   for(i = 1; i <= numberss; i++) // loop i to 5, 
   {
	  for(j = 1; j <= i; j++) // loop to count
		 cout << j << " "; // output the numbers
	  cout << endl; // next line output.
   }

	system("pause");
	return Success;

}//end of main.


*mod edit: added code tags :code:
helped with formatting for output



#include<iostream.h>
int main()
{
   int i, j;
   for(i=1; i<=5; i++)
   {
	   for(j=1; j<=5-i; j++)
		  cout<<" ";
	   for(j=1; j<=i; j++)
		  cout<<j<<" ";
	   cout<<"\n";
   }
   return 0;
}


**Mod Edit:added code tags**
Was This Post Helpful? 0
  • +
  • -

#5 OliveOyl3471  Icon User is offline

  • Everybody's crazy but me!
  • member icon

Reputation: 134
  • View blog
  • Posts: 6,581
  • Joined: 11-July 07

Re: C++ Christmas tree

Posted 06 January 2010 - 06:42 AM

Thanks for including your code, but if you read the post that you quoted, you can see that we ask members to use the code tags.

Also, do you have a question?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1