4 Replies - 6529 Views - Last Post: 15 September 2010 - 02:52 PM Rate Topic: -----

#1 Guest_Guest*


Reputation:

Printing Numbers in Pyramid

Posted 13 August 2010 - 12:27 AM

sir

i want to print these numbers but cant do it. can u pls help

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5
Is This A Good Question/Topic? 0

Replies To: Printing Numbers in Pyramid

#2 sacred   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 42
  • Joined: 31-October 08

Re: Printing Numbers in Pyramid

Posted 13 August 2010 - 12:55 AM

to the guest guest:
you see there are 5 rows and the last row goes up to 5;
in the 1st row numbers go up to 1
in the 2nd row numbers go up to 2
in the 3rd row numbers go up to 3....
this must give an idea to form nested for loops to solve your problem.. try a bit and show your work if you get errors

View Postsmashlee, on 26 September 2009 - 03:04 PM, said:

this should definitely get me going for a while, thanks!

for the pattern you can see the topic My link

This post has been edited by sacred: 13 August 2010 - 12:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_parmeet kaur*


Reputation:

Re: Printing Numbers in Pyramid

Posted 15 September 2010 - 10:43 AM

#include<stdio.h>

int main()
{
  int i,j
  for(i=1;i<=5;i++)
   {
     for(j=1;j<=i;j++)
     printf("%d",j);
     printf("/n");
   }
  getch();
  return 0;
}

This post has been edited by JackOfAllTrades: 15 September 2010 - 02:41 PM

Was This Post Helpful? 0

#4 Coding in the name of   User is offline

  • D.I.C Regular

Reputation: 67
  • View blog
  • Posts: 294
  • Joined: 07-July 10

Re: Printing Numbers in Pyramid

Posted 15 September 2010 - 02:09 PM

The shape you are building is like an isosceles triangle cut out of a square. So you know the coordinate of the peak of the pyramid will be (number_of_rows/2). Call this x. For the first line print (x-(1/2)) spaces. For the second line print (x-(2/2)) spaces, and so on, until (x-(number_of_rows/2) = 0). After the spaces you can write the numbers. You will probably need another loop for this. Note that you will get truncation from the division so the pyramid won't be perfectly symmetrical, this is because the final positions of the characters can only be integers. Note also that for row numbers over 9 the additional length of the other numbers will make it even less symmetrical.

I got this output from an implementation of this method:
    1
   12
   123
  1234
  12345
 123456
 1234567
12345678
123456789


This post has been edited by Coding in the name of: 15 September 2010 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12399
  • View blog
  • Posts: 45,538
  • Joined: 27-December 08

Re: Printing Numbers in Pyramid

Posted 15 September 2010 - 02:52 PM

Topic split. Please avoid necroposting.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1