Linux Bash Scipting II

Linux Bash Scipting Examples


Linux Mastery: Mastering the Linux Command Line | Udemy

 Print multiplication table of a number.

echo "Enter a number : "
read a
for i in {1..10}; do
echo "$a x $i = $(($a*$i))"
i=$((i+1))
done

Output:-



Calculate factorial of a given number.

echo "Enter a number : "
read num
f=1
for((i=2;i<=num;i++))
{
  f=$((f*i))
}
echo "Factorial of $num is $f"

Output:-


Print the sum of digits of any number.

echo "Enter a number : "
read num
n=$num
sum=0
while [ $num -gt 0 ];
do
k=$((num%10))
num=$((num/10))
sum=$((sum+k))
done
echo "Sum of digits of $n is $sum"

Output:-



Comments

Popular posts from this blog

Some tips about the right way to charge your phone

Software Basics

Some unique and weird things about Japan