Saturday, July 25, 2009

Use for loops to construct a program that displays a pyramid of numbers on the screen.?

Hints


You may nest two inner loops, one to print spaces and one to print numbers (0-9), inside an outer loop that steps down the screen from line to line.


ask user to enter any number from 0 to 9. if he enter 3 out put will be.


*****1


****2 2


***3 3 3


**4 4 4 4


In the above sample there will no'*' in real and it is not appear in screan . it will write only for submitting sample.if I do not write * prew of sample is not correct in appearence.








PLEASE GIVE COMPLETE SOLUTION. THANKS

Use for loops to construct a program that displays a pyramid of numbers on the screen.?
I wrote my solution in perl. It should easily translate to other computer languages. C would be a good bet. Unfortunately, it didn't format as nice when I copied it into the answer box. Like the author of the question, I used an asterisk in place of the space.





#!/usr/bin/perl


#


# A Yahoo Answers programming answer


# This program prints a pyramid with each line numbered


# with the number of blocks in each layer.


#





print "Enter a number between 1 and 9. ";





$NUMBER = (%26lt;STDIN%26gt;); # Read input from standard input





chomp ($NUMBER); # Get rid of the trailing carriage return





print "\nCreating a Pyramid $NUMBER rows high\n\n";





$ROW_NUM = 1; # $ROW_NUM will count the number of rows





# The outer loop sets the number of lines to print





for ($LINES = $NUMBER; $LINES %26gt; 0; $LINES--) {





# The inner loops print the leading spaces followed


# by the $ROW_NUM row numbers.





# Print the leading spaces in the line





for ($i = $LINES; $i %26gt; 1 ; $i--) {


print "*"; # Replace the '*' with a space if you'd like.


}





# Print the Row Number $ROW_NUM times.


# Note that I print $ROW_NUM followed by a space for


# a nice evenly shaped pyramid.





for ($j = $ROW_NUM; $j %26gt; 0; $j--) {


print "$ROW_NUM "; # A space follows $ROW_NUM


}





print "\n"; # Print a newline to start the next line.





$ROW_NUM++; # Increment the row number counter.


}



necklace

No comments:

Post a Comment