I remember those days when you have just pass out of college and while you go for interviews you were asked to display your programming skills.... Following are few of the examples which were asked ...... I guess now too!! 


1. Ask for a number and find if its a Prime number ?


#!/bin/sh
i=2
rem=1
echo -e "Enter a number: \c"
read num
if [ $num -lt 2 ]; then
 echo -e "$num is not prime\n"
 exit 0
fi

while [ $i -le `expr $num / 2` -a $rem -ne 0 ]; do
 rem=`expr $num % $i`
 i=`expr $i + 1`
done
if [ $rem -ne 0 ]; then
 echo -e "$num is prime\n"
else
 echo -e "$num is not prime\n"
fi

2. Find largest number from the given list of number?


echo "Enter number list:"
read -a num
count=${#num[@]}
for (( i=0 ; i<count ; i++ ))
do
  for (( j=$i ; j<count ; j++ ))
  do
  if [  ${num[$i]} -gt ${num[$j]}  ];
  then
  temp=${num[$i]}
  num[$i]=${num[$j]}
  num[$j]=$temp
  fi
  done
done
echo "The largest number is ${num[$count-1]}"

3. Find FIBONACCI series for given number?


#!/bin/sh
echo -n "Enter number:"
read num
num1=0
num2=1
echo -n "Fibonacci series: "
echo -n " $num1"
echo -n " $num2"
count=2

while [ $count -le $num ]
do
num3=`expr $num1 + $num2`
echo -n " $num3"
num1=$num2
num2=$num3
count=`expr $count + 1`
done
echo -e "\n"

4. Find Factorial for given number?

#!/bin/sh
read -p  "Enter number to get factorial:" num
fact=$num
for (( i=$num-1 ; i>=1 ; i-- ))
do
fact=`expr $fact \* $i`
done
echo "Factorial for the number $num is $fact"

5. Sort for given range of number?

#!/bin/sh
# Sorting a given numbers
#
echo "Enter Numbers:"
read -a num
if [ ${#num[@]} == 0 ]
   then
   echo "You have not entered any numbers"
fi
count=${#num[@]}
echo "count: $count"
for ((i=0;i<count-1;i++))
 do
  for ((x=i;x<count;x++))
        do
        if [ ${num[i]} -gt ${num[x]} ]
        then
        temp=${num[i]}
        num[i]=${num[x]}
        num[x]=$temp
        fi
        done
 done
echo "The sorted number : ${num[@]}"

6. Removing duplicate number from a given list of number:

#!/bin/sh
echo "Enter list of numbers:"
read -a  num
count=${#num[@]}
x=$count
i=0
j=0
y=0
while (( i < x )); do
        (( j = i + 1 ))
        while (( j < x )); do
                if [ ${num[i]} = ${num[j]} ]; then
                   break                fi
                (( j = j + 1 ))
        done

        if [ $j = $x ]; then
            newarr[y]=${num[i]}
            (( y = y + 1 ))
        fi
        (( i = i + 1 ))
done
echo "The list without duplicate numbers:${newarr[@]}"




/**/