Get Update on recent Technology & Programming

Showing posts with label Shell Scripting. Show all posts
Showing posts with label Shell Scripting. Show all posts

Sunday, 29 October 2017

Shell Script to Print Patterns


pattern 1 :
     *  
    * *  
   * * *  
  * * * *  
 * * * * *  
 * * * * *  
  * * * *  
   * * *  
    * *  
     * 
1:  i=1  
2:  while [ $i -le 5 ]  
3:  do  
4:  j=5  
5:  while [ $j -ge $i ]  
6:  do  
7:   echo -n " "  
8:   j=`expr $j - 1`  
9:  done  
10:  j=1  
11:  while [ $j -le $i ]  
12:  do  
13:   echo -n "*"  
14:   echo -n " "  
15:   j=`expr $j + 1`  
16:  done  
17:  echo " "  
18:  i=`expr $i + 1`  
19:  done  
20:  i=1  
21:  while [ $i -le 5 ]  
22:  do  
23:  j=1  
24:  while [ $j -le $i ]  
25:  do  
26:   echo -n " "  
27:   j=`expr $j + 1`  
28:  done  
29:  j=5  
30:  while [ $j -ge $i ]  
31:  do  
32:   echo -n "*"  
33:   echo -n " "  
34:   j=`expr $j - 1`  
35:  done  
36:  echo " "  
37:  i=`expr $i + 1`  
38:  done  

pattern 2 :

1 
22 
333 
4444 
55555 
1:  i=1  
2:  while [ $i -le 5 ]  
3:  do  
4:  j=1  
5:  while [ $j -le $i ]  
6:  do  
7:   echo -n $i  
8:   j=`expr $j + 1`  
9:  done  
10:  echo " "  
11:  i=`expr $i + 1`  
12:  done  
pattern 3 :

1 
12 
123 
1234 
12345 
1:  i=1  
2:  while [ $i -le 5 ]  
3:  do  
4:  j=1  
5:  while [ $j -le $i ]  
6:  do  
7:   echo -n $j  
8:   j=`expr $j + 1`  
9:  done  
10:  echo " "  
11:  i=`expr $i + 1`  
12:  done  

Shell Script Arithmetic Operations

Shell Script Arithmetic Operations


 echo "Enter first number :-"  
 read num1  
 echo "Enter second number :-"  
 read num2  
 ans=`expr $num1 + $num2`  
 echo "Addition is : $ans"  
 ans=`expr $num1 - $num2`  
 echo "Substraction is : $ans"  
 ans=`expr $num1 \* $num2`  
 echo "Multiplication is : $ans"  
 ans=`expr $num1 / $num2`  
 echo "Division is : $ans"  
 ans=`expr $num1 % $num2`  
 echo "Modulus is : $ans"  

Shell Script Program To Print Prime Numbers upto a Given Number

Shell Script Program To Print Prime Numbers upto a Given Number




 echo Enter a number till which you want to print the prime numbers  
 read num  
 c=2  
 while [ $c -le $num ]  
 do  
 flag=1  
 i=2  
 while [ $i -lt $c ]  
 do  
  a=`expr $c % $i`  
  if [ $a -eq 0 ]  
  then  
  flag=0  
  fi  
  i=`expr $i + 1`  
 done  
 if [ $flag -ne 0 ]  
 then  
  echo $c  
 fi  
 c=`expr $c + 1`  
 done  

Shell Script Program To Check Number is Prime Or Not

Shell Script Program To Check Number is Prime Or Not



 echo Enter a number  
 read num  
 i=2  
 flag=1  
 while [ $i -lt $num ]  
 do  
  a=`expr $num % $i`  
  if [ $a -eq 0 ]  
  then  
  flag=0  
  fi  
  i=`expr $i + 1`  
 done  
 if [ $flag -eq 0 ]  
 then  
  echo $num is not a Prime number  
 else  
  echo $num is a Prime number  
 fi  

Shell Script Program To Find Factorial of Any Number

Shell Script Program To Find Factorial of Any Number 




 echo Enter a number  
 read num  
 i=1  
 fact=1  
 while [ $i -le $num ]  
 do  
  fact=`expr $fact \\* $i`  
  i=`expr $i + 1`  
 done  
 echo Factorial of $num is $fact  

Shell Script Program To Check Number is Even Or Odd

Shell Script Program To Check Number is  Even Or Odd





 echo "Enter a number"  
 read num  
 rem=`expr $num % 2`  
 if [ $rem -eq 0 ]  
 then  
  echo "$num is Even"  
 else  
  echo "$num is Odd"  
 fi  
Hey, we've just launched a new custom color Blogger template. You'll like it -
Join Our Newsletter