12 January 2014

Shell Script to print Pyramid of Numbers

21 comments


Q. How do I print pyramid of numbers in Bash


Ans.
how to print number pyramid in ubuntu

Before writing bash script lets understand how we going to print this pattern. We do it in two part, first we are going to print part 1 and then we print the part 2. As you can notice from above figure that, in 2nd part we are printing number in reverse order till 1.

#!/bin/bash

#Taking input
read -p "Enter Number:" number

#Outer loop for printing number of rows in pyramid
for((row=1;row<=number;row++))
do

    #Loop for printing required spaces
    for((spaces=row;spaces<=number;spaces++))
    do
        echo -ne " "
    done

    #Loop for printing 1st part
    for((j=1;j<=row;j++))
    do
        echo -ne "$j"
    done

    #Loop for printing 2nd part
    for((l=(row-1);l>=1;l--))
    do
        echo -ne "$l"
    done

    #echo for printing new line
    echo 
done
    

Output


how to print number pyramid in bash script

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

21 comments:

  1. Replies
    1. same as
      *
      **
      ***
      ****
      for above pyramid we print "row" alphabet.. to get the pyramid asked by you should print the "2nd" alphabet and initiate it by "1".

      Delete
    2. #!/bin/bash
      echo "enter the n value"
      read n
      for((i=1;i<=n;i++))
      do
      for((j=1;j<=i;j++))
      do
      echo -n $j " "
      done
      echo
      done

      Delete
  2. printNum()
    {
    for((i=1;i<=n;i++))
    do
    for((k=1;k<=i;k++))
    do
    echo -ne "$k";
    done
    echo
    done
    }

    ReplyDelete
  3. How to print:
    0
    0 2
    0 2 4
    0 2 4 16
    0 2 4 16 256

    ReplyDelete
    Replies
    1. I have posted a relevent answer to your question. Correct me if I'm wrong. Happy to help :P

      Delete
    2. I have posted a relevent answer to your question. Correct me if I'm wrong. Happy to help :P

      Delete
  4. #!/bin/bash

    read -p "N? : " n

    for((i = 0; i < n; i++))
    do
    k=0
    key=0
    while((k < $((i+1))))
    do
    if((k == 0))
    then
    echo -e "$key \c"
    key=2
    k=$((k+1))
    else
    echo -e "$key \c"
    key=$(echo "$key^2" | bc)
    k=$((k+1))
    fi
    done

    echo " "
    done

    ReplyDelete
  5. #!/bin/bash

    read -p "N? : " n

    for((i = 0; i < n; i++))
    do
    k=0
    key=0
    while((k < $((i+1))))
    do
    if((k == 0))
    then
    echo -e "$key \c"
    key=2
    k=$((k+1))
    else
    echo -e "$key \c"
    key=$(echo "$key^2" | bc)
    k=$((k+1))
    fi
    done

    echo " "
    done

    ReplyDelete
  6. can any one help me print the following pattern :-

    54321
    4321
    321
    21
    1

    ReplyDelete
  7. your shell script program is too easy and understand please share more program
    write for us

    ReplyDelete
  8. If you are looking for the Software Engineering Help assignment then in this case you can opt for our Essay Writing.we provide the best online assignment help.We also provide law essay help for students across the globe. for more information contact us +16692714848.


    ReplyDelete
  9. Thanks a lot for the kind of perfect topic I have not a lot of information about it but I have got an extra Yellowstone rip wheeler vest unique info in your unique post.Perfect description you have provided thanks for sharing.

    ReplyDelete
  10. Replies
    1. Write a shell script which will accept the number of row as input and will print the
      following output (using loop)
      1
      2 8
      3 12 27
      4 16 36 64

      Delete
  11. The Irritable Bowel Syndrome Treatment Market size was valued at $1,071 million in 2018, and is estimated to reach $2,012 million by 2026, registering a CAGR of 8.2% from 2019 to 2026. Irritable bowel syndrome (IBS) is a common gastrointestinal disorder that affects the large intestine. Signs & symptoms include abdominal pain, bloating, gas, cramping, and constipation or diarrhea. There are three types of irritable bowel syndrome such as IBS with diarrhea (IBS-D), IBS with constipation (IBS-C), and Mixed IBS. Treatment of irritable bowel syndrome consists of stress relief, change in diet, proper medicines, and counselling. Medications available for the treatment of irritable bowel syndrome include rifaximin, eluxadoline, lubiprostone, linaclotide, and others.

    ReplyDelete
  12. This comment has been removed by the author.

    ReplyDelete