how to print triange using while loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to print triange using while loop
# 1  
Old 07-26-2012
how to print triange using while loop

Hi

pls tell me how to print a print triangle as below
Code:
   
     0
    101
   21012

i am able to print triangle as below
Code:
0 
1 0 
2 1 0 
3 2 1 0 
4 3 2 1 0 
5 4 3 2 1 0 
6 5 4 3 2 1 0 
7 6 5 4 3 2 1 0 
8 7 6 5 4 3 2 1 0 
9 8 7 6 5 4 3 2 1 0

the script which i used for above o/p is mention as below
Code:
#!/bin/ksh
x=0
while [ "$x" -lt 10 ] ; # this is loop1
do
y="$x"
while [ "$y" -ge 0 ] ; # this is loop2
do
echo "$y \c"
y=`echo "$y - 1" | bc`
done
echo
x=`echo "$x + 1" | bc`
done

---------- Post updated at 04:27 PM ---------- Previous update was at 03:15 PM ----------

help me pls
# 2  
Old 07-26-2012
Bug

Code:
var=10
for ((I=1; I <= $var ; I++));
do
md=`expr $var - $I + 1`

for((i=1;i<$md;i++));do printf "%s" " ";done;
for((k=$md;k<=$var;k++));do printf "%s" "$k";done;
for((t=($var-1);t>=$md;t--));do printf "%s" "$t";done;printf "\n"

done

output is--

Code:
         10
        9109
       891098
      78910987
     6789109876
    567891098765
   45678910987654
  3456789109876543
 234567891098765432
12345678910987654321

# 3  
Old 07-26-2012
thx for the response
but i need to create only using while loop
# 4  
Old 07-27-2012
Java

hi

i need to know how to print triangle using while loop only.
need to know in simple form as i am a beginner to shell
# 5  
Old 07-27-2012
This is clearly homework. Closed
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Loop through directory and print on line

In the bash below I am trying to loop through all the R1.gz in a directory (always 1), store them in ARRAY, and cut them before the second _. That is being done but I can't seem to print then one a single line seperated by a space. Is the below the best way or is there a better solution? Thank you... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Print file names in a loop

OS : RHEL 6.1 Shell : Bash I have lots of files in /tmp/stage directory as show below. Using a loop, I need to print all the filenames in this directory except those ending with a number. How can I do this ? # pwd /tmp/stage # # # ls -l * -rw-r--r--. 1 root root 0 Oct 7 18:38 stmt1... (2 Replies)
Discussion started by: kraljic
2 Replies

3. Shell Programming and Scripting

For loop and awk print

Hi, i am using a variable tmpVar, using variable data i need implement for loop tmpVar="abc bbc cbc nbc mbc" # valiable having total number of words= 5 so i need to loop 5 times. tmpVar="abc bbc cbc nbc mbc" tmpcnt=`echo $tmpVar|wc -w` for cnt in 1..$tmpcnt do t1=`echo... (4 Replies)
Discussion started by: onesuri
4 Replies

4. Shell Programming and Scripting

Print in New line in loop

Hi , i want to print the output in line by line while read LINE do echo $LINE | grep UCM | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRBr | egrep '(Shutdown|Unavailable)' echo $LINE | grep SRP| egrep '(Shutdown|Unavailable)' echo $LINE | grep OM | grep JMS|... (7 Replies)
Discussion started by: navsan
7 Replies

5. Shell Programming and Scripting

For Loop And Print 2 Variables

I have the following loop: for I in `ldm ls |grep -v ^NAME| awk '{print $1}'` do ldm ls -o network $I | echo $I | tr -s ' ' | cut -f6 -d " " | more +9 I would like the following displayed to a file: The value of $I, followed by a newline, then the information from the rest of the... (3 Replies)
Discussion started by: hxman
3 Replies

6. Shell Programming and Scripting

loop + sum + print using awk

Hi, I am unable sum of each column in the loop usng awk command. Awk is not allowing the parameters in the command. i am facing the below error. awk: 0602-562 Field $() is not correct. Source file abc.txt 100,200,300,400,500,600,700,800,900 101,201,301,401,501,601,701,801,901 ... (1 Reply)
Discussion started by: number10
1 Replies

7. Shell Programming and Scripting

Print filename inside loop

Im want to print filename inside loop .. the code im using :- Filename_1=abc_20090623_2.csv.lk Filename_2=def_20090623_2.csv.lk i want to extract filename till .csv eg Filename_1=abc_20090623_2 Filename_2=def_20090623_2 How can i do this inside the for loop ... (3 Replies)
Discussion started by: r_t_1601
3 Replies
Login or Register to Ask a Question