Skip to main content

Learn Programming in UNIX



Q: - Write a program to input two numbers and add them?

            gedit add.sh
            clear
            echo “enter two numbers:”
            read a
            read b
            sum=`expr$a+$b`
            echo $sum


Syntax for if-else:-

(1)         if test condition
            then
                ---------
                ---------
            else
                ---------
                ---------
            fi

(2)         if[condition]
            then
               ---------
                ---------
            else
                ---------
                ---------
            Fi

Q: - Write a program to input a numbers and check it is negative or not?

            clear
            echo “enter a numbers:”
            read a
            if[$a-ge 0]
            then
            echo “no. is positive”
            else
            echo “no. is negative”
            fi







Q: - Write a program to input a numbers and check it is even or odd?
            clear
            echo “enter a numbers:”
            read a
            r= `expr $a%2`
            if test $r –eq 0
            then
            echo “no. is even”
            else
            echo “no. is odd”
            fi
Q: - Write a program to input two numbers and swap them?       
         clear
echo "enter the value of a :"
read a
echo "enter the value of b"
read b
t=$a
a=$b
b=$t
echo " value of a : $a"
echo " value of b: $b"

Q: - Write a program to input two numbers and find greatest number?

         clear
echo "enter the value of a :"
read a
echo "enter the value of b"
read b
if test $a -gt $b
then
echo "largest  value: $a"
else
echo "largest  value: $b"
fi



Q: - Write a program to input a numbers and check number is positive or not?

clear
echo "enter the value of a :"
read a
if test $a -ge 0
then
echo "no. positive"
else
echo "no. negative"
fi


Syntax for ‘for’ loop:-

         For((initial;sondition;increment/decrement))
    Do
         --------------------------------
         --------------------------------
         --------------------------------
    Done


Q: - Write a program to input a numbers and print the table of that number?

        
clear
echo "enter any no.:"
read a
for((i=1;i<=10;i++))
do
t=`expr $a \* $i`
echo " $a * $i " $t
done


Syntax for ‘while’ loop:-


    While test condition
    Do
         ------------
         ------------
         ------------
    Done

Q: - Write a program to input a numbers and print digit by digit?

   
clear
echo "enter any no.:"
read a
while test $a -gt 0
do
t=`expr $a % 10`
echo $t
a=`expr $a / 10`
done

Q: - Write a program to input a numbers and add every digit?

clear
echo "enter any no.:"
read a
while test $a -gt 0
do
t=`expr $a % 10`
s=`expr $s + $t`
a=`expr $a / 10`
done
echo "sum of every digit: $s"


Syntax for switch case:-

    Case $variable name in
         1)
             --------
             --------
             --------
             --------
             --------
             --------
             ;;
         2)
             --------
             --------
             --------
--------
             --------
             --------

             ;;
        
         *)
             --------
             --------
             --------
--------
             --------
             --------

             ;
    Esac


        




Example for switch case:-
clear
echo "1)addition"
echo "2)substraction"
echo "3)division"
echo "4)multiplication"
echo "enter two number:"
read a
read b
echo "enter your choice:"
read ch
case $ch in
1) result=`expr $a + $b`;;
2) result=`expr $a - $b`;;
3) result=`expr $a / $b`;;
4) result=`expr $a \* $b`;;
*) echo "invalid choice"
esac
echo $result
Q: - Write a program to input a numbers and check no. is perfect or not?
clear
echo "enter a no:"
read a
sum=0
mid=`expr $a/2`
for((i=1;i<=$mid;i++))
do
r=`expr $a % $i`
if test $r -eq 0
then
sum=`expr $sum + $i`
fi
done
if test $sum -eq $a
then
echo "no is perfect"
else
echo "not perfect"
fi

Q: - Write a program to input a numbers and check no. is prime or not?
clear
echo "enter a no:"
read a
f=0
for((i=1;i<=$a;i++))
do
r=`expr $a % $i`
if test $r -eq 0
then
f=`expr $f + 1`
fi
done
if test $f -eq 2
then
echo "no is prime"
else
echo "not prime"
fi
Q: - Write a program to input a numbers and check no. is Armstrong or not?
clear
echo "enter a no:"
read a
sum=0
f=$a
while test $a -ne 0
do
r=`expr $a % 10`
a=`expr $a / 10`
sum=`expr $sum + $r \* $r \* $r`
done
if test $sum -eq $f
then
echo "no is armstrong"
else
echo "not armstrong"
fi
Q: - Write a program to print n term Fibonacci series?
clear
echo "how many terms:"
read a
f=0
p=1
echo $f
echo $p
d=`expr $a - 2`
for((i=1;i<=d;i++))
do
next=`expr $f + $p`
f=$p
echo $next
p=$next
done

Q: - Write a program to input a number and find its factorial?

clear
echo "enter a no:"
read n
fact=1
for((i=$n;i>=1;i--))
do
fact=`expr $fact \* $i`
done
echo "factorial is = $fact"

Q: - Write a program to input a file name and display its contents?
clear
echo "enter file name:"
read s
cat $s




Q: - Write a program to input a file name and display its contents in reverse ordered?

clear
echo "enter file name:"
read s
rev $s



Q: - Write a program to input a file name and encrypt it in to a file name encrypt.txt?

clear
echo "enter file name:"
read fn
tr[a-z A-Z 0-9][d-z c-x 1-5]<$fn>encrypt.txt
cat $encrypt.txt




Q: - Write a program to input a string and find the length?

clear
echo "enter a string:"
read a
len=`echo $a|wc -c`
len=`expr $len - 1`
echo $len








Q: - Write a program to input two strings and concatenate them?

clear
echo "enter two strings:"
read a
read b
cat $a + $b > $c
echo $c

Command line argument

    $# = Total number of argument

    $1 = First argument

    $2 = Second argument

    $* = Total argument to print/show



Q: - Write a program to input a file name  with CLA and display the content?

Clear
Cat $1


Q: - Write a program to print all the arguments?

Clear
Echo $*

Comments

Popular posts from this blog

Multiple Inheritance

Multiple Inheritance In multiple inheritance a class can access the property of more than one class. This inheritance is use for multiple accessing of properties. In multiple inheritance ambiguty is  occured when same name function is declare in both class and the object is confuse with function is called.  syntax − class derived-class: access baseA, access baseB.... Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following  example − #include <iostream.h> #include <conio.h>  // Base Class class Shape  {    public:       void getwidth(int w) {          width = w;       }       void getheight(int h) {          height = h;       }           prot...

CODD RULES of RDBMS

Codd’s rules for RDBMS (i)                 The information rule. (ii)              Guaranteed access of   data (iii)            Systematic treatment of null value. (iv)            On line DB support. (v)              Comprehensive sup-language rule. (vi)            The view update rule. (vii)         Insert update rule. (viii)       Physical data independence. (ix)            Logical data independence. (x)              Integrity rule (xi)  ...