English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2006-10-26 03:01:31 · 1 answers · asked by Muhammad A 1 in Education & Reference Higher Education (University +)

1 answers

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 = (); # 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 > 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 > 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 > 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.
}

2006-10-26 13:42:09 · answer #1 · answered by DavidNH 6 · 0 0

fedest.com, questions and answers