Computer Project
Computer Project
PAPER : CC/GE-4(CMS-G-CC-4-4-P)
CU ROLL : 213023-21-0006
Serial Page
Program
No. No.
Ask the user to enter two numbers and output the sum,
4 6
product, difference, and GCD.
28
14 Write a shell script to print Pascal diamond.
30
15 Acknowledgement
1. Write a shell script to find the factorial of any number.
ALGORITHM:
Step 1: Start
Step 4: Calculate
fact = fact * i
Step 7: Stop
CODE:
echo “Enter a number”
read i
fact=1
while [ $i -gt 0 ]
do
let fact=fact*i
let i- -
done
echo "The factorial of the number is $fact"
OUTPUT:
1
2. Write a shell script to display Fibonacci series up to
10 terms.
ALGORITHM:
Step 5: End
CODE:
a=0
b=1
echo "Enter the range"
read range
echo "The fibonacci series is"
echo $a
for (( i=2; i<=$range; i++ ))
do
c=`expr $a + $b`
a=$b
b=$c
echo $a
done
2
OUTPUT:
3
3. Write a shell script to reverse a given integer number.
ALGORITHM:
Step 1: Initialize a variable `number` to store the given
integer.
Step 2: Initialize a variable `reversedNumber` to 0.
Step 3: Repeat the following steps until `number` becomes 0:
Step 3.1: Get the last digit of `number` by taking
the modulo (%) with 10.
Step 3.2: Append the last digit to `reversedNumber`
by multiplying it with 10 and adding to
`reversedNumber`.
Step 3.3: Reduce `number` by removing the last digit,
i.e., `number = number / 10` (integer
division).
Step 4: The reversed number is stored in the variable
`reversedNumber`.
Step 5: End.
CODE:
echo “Enter the number”
read a
s=0
while [ $a -gt 0 ]
do
r=`expr $a % 10`
s=`expr $s \* 10 + $r`
a=`expr $a / 10`
done
echo “reverse of a given number is $s”
4
OUTPUT:
5
4. Ask the user to enter two numbers, and output the
sum, product, difference, and the GCD.
ALGORITHM:
Step 1: Read the first number from the user and store it in
variable `a`.
Step 2: Read the second number from the user and store it
in variable `b`.
Step 9: End.
CODE:
echo "Enter the 1st number"
read a
echo "Enter the 2nd number"
read b
c=` expr $a + $b `
echo "The sum of the two given number is $c"
d=`expr $a - $b `
echo "The difference of the two given number is $d"
p=`expr $a \* $b `
echo "The product of the two number is $p"
for (( i=1; i<=a; i++ ))
do
6
if (( a%i==0 && b%i==0 ))
then
gcd=$i
fi
done
echo "The GCD of the two given number is $gcd"
OUTPUT:
7
5. Write a program to swap two variables using
temporary variables.
ALGORITHM:
Step 6: End.
CODE:
8
OUTPUT:
9
6. Write a program to swap two variables without using
temporary variables.
ALGORITHM:
Step 2: Read the input value from the user and store it in
a variable `a`.
Step 4: Read the input value from the user and store it in
a variable `b`.
Step 8: End.
CODE:
echo "Enter the 1st number"
read a
echo "Enter the 2nd number"
read b
echo "Before swapping the 1st number is $a and the 2nd
number is $b"
a=$(( a + b ))
b=$(( a - b ))
a=$(( a - b ))
10
echo "After swapping the 1st number is $a and the 2nd
number is $b"
OUTPUT:
11
7. Write a shell script to check the input year is leap
year or not.
ALGORITHM:
Step 1: Start
Step 8: Stop
CODE:
echo "Enter the year"
read year
if (( $year % 4 ==0 && $year % 100 !=0)) || (( $year % 400
==0 ))
then
echo "The given year is a leap year"
else
echo "The given year is not a leap year"
fi
12
OUTPUT:
13
8. Write a program to check if a number is odd or even.
ALGORITHM:
Step 1: START
Step 6: END
CODE:
echo "Enter a number"
read n
if (( $n % 2 == 0 ))
then
echo “The number is even”
else
echo "The number is odd"
fi
OUTPUT:
14
15
9. Write a shell script to reverse a string and check
whether it is a palindrome.
ALGORITHM:
Step 2: Read the input string from the user and store it in
a variable `s`.
CODE:
echo enter the string
read s
16
echo $s>temp
rvs="$(rev temp)"
if [ $s = $rvs ]
then
echo it is palindrome
else
echo it is not palindrome
fi
OUTPUT:
17
10.Write a shell script to find a number using sequential
search method.
ALGORITHM:
Step 8: End.
CODE:
echo "Enter the number of elements"
read n
echo "Enter the array elements"
for (( i=1;i<=n;i++ ))
do
read a[$i]
done
echo "Enter the element to be search"
read p
j=1
while [ $j -lt $n -a $p -ne ${a[$j]} ]
do
18
j=`expr $j + 1`
done
if [ $p -eq ${a[$j]} ]
then
echo "$p is present at location $j"
else
echo "$p is not present in the array"
fi
OUTPUT:
19
11.Write a shell script to sort a set of integer numbers
using bubble sort.
ALGORITHM:
Step 9: End.
CODE:
echo "Enter the number of elements"
read n
echo "Enter the elements in the array"
for (( i=0;i<$n;i++ ))
do
read a[$i]
done
20
echo "Numbers in the array are:"
for (( i=0;i<$n;i++ ))
do
echo ${a[$i]}
done
for (( i=0;i<$n;i++ ))
do
for (( j=$i;j<$n;j++ ))
do
if [ ${a[$i]} -gt ${a[$j]} ]
then
t=${a[$i]}
a[$i]=${a[$j]}
a[$j]=$t
fi
done
done
echo "The sorted numbers in ascending order are:"
for ((i=0;i<$n;i++ ))
do
echo ${a[$i]}
done
OUTPUT:
21
12.Write a shell script to display all the prime numbers
within a range.
ALGORITHM:
Step 2: Read the lower limit `m` and the upper limit `n`
from the user.
Step 5: End.
CODE:
echo Enter a Range
read m n
echo Prime Numbers between $m and $n :
for (( a=m;a<=n;a++ ))
do
k=0
for (( i=2;i<a;i++ ))
do
if [ $((a % i)) -eq 0 ]
then
k=1
break
fi
done
if [ $k -eq 0 ]
then
22
echo $a
fi
done
OUTPUT:
23
13.Write a shell script to find the roots of a quadratic
equation ax2 + bx +c = 0, considering all possible
cases.
ALGORITHM:
Step 6: End
24
CODE:
echo coefficient of X^2
read a
echo Coefficient of X^1
read b
echo Coefficient of X^0
read c
if [ $a == 0 ]
then
echo Coefficient of X^2 can not be 0
else
disc=$(($b*$b - 4*$a*$c))
echo Discriminant $disc
if (( $disc > 0 ))
then
root1=$(awk "BEGIN {print ((-1*$b) + sqrt($disc)) /
(2*$a)}")
root2=$(awk "BEGIN {print ((-1*$b) - sqrt($disc)) /
(2*$a)}")
echo The two distinct real roots are $root1 and $root2
elif [[ $disc == 0 ]];
then
root1=$(awk "BEGIN {print (-1*$b) / (2*$a)}")
echo Both the roots are $root1
else
real=$(awk "BEGIN {print (-1*$b) / (2*$a)}")
img_root=$(awk "BEGIN {print sqrt((-1*$disc)) / (2*$a)}")
if [ $img_root == 1 ]
then
echo The two imaginary roots are $real + i and $real - i
else
echo The two imaginary roots are $real + $img_root i and
$real - $img_root I
fi
fi
fi
OUTPUT:
25
26
27
14.Write a shell script to print Pascal diamond.
ALGORITHM:
Step 5: Use a loop from 'i = mid_row - 1' to 'i >= 1'
(inclusive) to print the second half of the Pascal diamond.
Step 6: End
CODE:
echo "enter value of n "
read num
for (( i=1;i<=$num ;i++))
28
do
for (( j=$num;j>=i;j-- ))
do
echo -n " "
done
for (( c=1;c<=i;c++ ))
do
echo -n " *"
sum=`expr $sum + 1`
done
echo ""
done
d_max=`expr $num - 1`
for (( i=$d_max;i>=1;i--))
do
for (( j=i;j<=$d_max;j++ ))
do
if [ $j -eq $d_max ]
then
echo -n " "
fi
echo -n " "
done
for (( c=1;c<=i;c++ ))
do
echo -n " *"
sum=`expr $sum + 1`
done
echo ""
done
OUTPUT:
29
: Acknowledgement :
...………………………………………
ARJYA SHARMA
30