3 Replies - 2029 Views - Last Post: 03 July 2013 - 06:43 PM Rate Topic: -----

#1 winniekaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-July 13

print upside down triangle with numbers

Posted 03 July 2013 - 04:04 PM

right now my code prints like this

Enter number of rows: 5
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Press any key to continue . . .

i want the numbers to display like this :
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

please help!! :helpsmilie:
#include<iostream>
using namespace std;

int main()
{
	int rows, k=0;
	cout << "Enter number of rows: ";
	cin >> rows;

	for(int i =rows; i>=1; i--)
	{
		for(int j=i; j>=1; j--)
			cout << j+k << " " ; 
			++k;
		
		cout << endl;
	}
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: print upside down triangle with numbers

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3449
  • View blog
  • Posts: 10,646
  • Joined: 05-May 12

Re: print upside down triangle with numbers

Posted 03 July 2013 - 04:17 PM

C++ is not like Python where indents are significant. You'll need to put curly braces around the blocks of code that you intend to be run within a loop. In other words, look closely at your lines 12-14.
Was This Post Helpful? 0
  • +
  • -

#3 winniekaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-July 13

Re: print upside down triangle with numbers

Posted 03 July 2013 - 04:25 PM

View PostSkydiver, on 03 July 2013 - 04:17 PM, said:

C++ is not like Python where indents are significant. You'll need to put curly braces around the blocks of code that you intend to be run within a loop. In other words, look closely at your lines 12-14.


could you give me more hints to make it print the other way please :unsure:

#include<iostream>
using namespace std;

int main()
{
	int rows, k=0;
	cout << "Enter number of rows: ";
	cin >> rows;

	for(int i =rows; i>=1; i--)
	{
		for(int j=i; j>=1; j--)
		{
			cout << j+k << " " ; 
		}
		++k;
		cout << endl;
		
	}

	return 0;
}




Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,298
  • Joined: 25-December 09

Re: print upside down triangle with numbers

Posted 03 July 2013 - 06:43 PM

Have you tried taking the answer given in this topic and reversing it?

Programming requires thinking, maybe you should try turning off your computer and use paper and pencil to write out some pseudo code then try to program it. If you can't do this easy task manually you'll never be able to write the program.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1