[go: up one dir, main page]

0% found this document useful (0 votes)
56 views32 pages

Computer Project

The document contains 15 programming problems to be solved using shell scripting. For each problem, it provides the algorithm, sample code, and example output. The problems cover topics like finding the factorial or Fibonacci sequence, reversing numbers, comparing and swapping variables, checking for palindromes, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views32 pages

Computer Project

The document contains 15 programming problems to be solved using shell scripting. For each problem, it provides the algorithm, sample code, and example output. The problems cover topics like finding the factorial or Fibonacci sequence, reversing numbers, comparing and swapping variables, checking for palindromes, and more.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

4th SEMESTER, 2023

NAME : Arjya Sharma

SUBJECT : Computer Science(General)

PAPER : CC/GE-4(CMS-G-CC-4-4-P)

CU ROLL : 213023-21-0006

CU REG NO. : 023-1111-0263-21


-: Table Of Contents :-

Serial Page
Program
No. No.

1 Write a shell script to find the factorial of any number. 1

Write a shell script to display Fibonacci series up to 10


2 2
terms.

3 Write a shell script to reverse a given integer number. 4

Ask the user to enter two numbers and output the sum,
4 6
product, difference, and GCD.

Write a program to swap two variables using temporary


5 8
variables.

Write a program to swap two variables without using


6 10
temporary variables.

7 Write a program to check the input year is leap year or not. 12

8 Write a program to check if a number is odd or even. 14

Write a shell script to reverse a string and check whether it


9 16
is a Palindrome.

Write a shell script to find a number using Sequential


10 18
search method.

Write a shell script to sort a set of integer numbers using


11 20
Bubble Sort.

Write a shell script to display all the Prime


12 22
Numbers within a range.

Write a shell script to find the roots of a quadratic equation


13 24
𝑎𝑋2 + 𝑏𝑋 + 𝑐 = 0 , considering all possible cases.

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 2: Read a number n

Step 2: Initialize variables: i = 1, fact = 1

Step 3: if i <= n go to step 4 otherwise go to step 7

Step 4: Calculate

fact = fact * i

Step 5: Increment the i by 1 (i=i+1) and go to step 3

Step 6: Print fact

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 1: Initialize term1, term2, next_term as integers

Step 2: Set term1 = 0 and term2 = 1

Step 3: Print term1 and term2

Step 4: Repeat the following steps from i = 3 to 10:

Step 4.1: Set next_term = term1 + term2

Step 4.2: Print next_term

Step 4.3: Set term1 = term2

Step 4.4: Set term2 = next_term

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 3: Calculate and print the sum of `a` and `b`.

Step 4: Calculate and print the difference between `a` and


`b`.

Step 5: Calculate and print the product of `a` and `b`.

Step 6: Initialize a variable `gcd`

Step 7: Use a loop from `i = 2` to `i <= a`.


Step 7.1: If both `a` and `b` are divisible by
`i`, update the value of `gcd` to `i`.

Step 8: Print "The GCD of the two given numbers is gcd",


where `gcd` is the calculated Greatest Common
Divisor.

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 1: Initialize two variables `var1` and `var2` with


their respective values.

Step 2: Initialize a temporary variable `temp` to store the


value during the swap process.

Step 3: Print the initial values of `var1` and `var2`.

Step 4: Perform the swap using the temporary variable:

Step 4.1: Set `temp` equal to the value of `var1`.


Step 4.2: Set `var1` equal to the value of `var2`.
Step 4.3: Set `var2` equal to the value of `temp`.

Step 5: Print the swapped values of `var1` and `var2`.

Step 6: End.

CODE:

echo "Enter the 1st number"


read a
echo "Enter the 2nd number"
read b
echo "Before swapping"
echo "The 1st number is $a"
echo "The 2nd number is $b"
temp=$a
a=$b
b=$temp
echo "After swapping"
echo "The 1st number is $a"
echo "The 2nd number is $b"

8
OUTPUT:

9
6. Write a program to swap two variables without using
temporary variables.

ALGORITHM:

Step 1: Print "Enter the 1st number".

Step 2: Read the input value from the user and store it in
a variable `a`.

Step 3: Print "Enter the 2nd number".

Step 4: Read the input value from the user and store it in
a variable `b`.

Step 5: Print "Before swapping the 1st number is {a} and


the 2nd number is {b}".

Step 6: Swap the values of `a` and `b` without using a


temporary variable:

Step 6.1: Set `a` to the sum of `a` and `b`.


Step 6.2: Set `b` to the difference between the
updated value of `a` and the original
value of `b`.
Step 6.3: Set `a` to the difference between the
updated value of `a` and the updated
value of `b`.

Step 7: Print "After swapping the 1st number is {a}


and the 2nd number is {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 2: Read year

Step 3: If the year is divisible by 4 then go to Step 4


else go to Step 7

Step 4: If the year is divisible by 100 then go to Step 5


else go to Step 6

Step 5: If the year is divisible by 400 then go to Step 6


else go to Step 7

Step 6: Print "Leap year"

Step 7: Print "Not a leap year"

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 2: Receive data from a user or dynamically assign


value.

Step 3: if num percent 2 == 0.Even Integer should be


printed.
Step 4: Else, Print an Odd Number

Step 5: Print an Odd Number

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 1: Print "Enter a string".

Step 2: Read the input string from the user and store it in
a variable `s`.

Step 3: Write the string `s` to a temporary file named


"temp".

Step 4: Reverse the content of the file "temp" and store it


in a variable `rvs`.

Step 5: Compare the original string `s` with the reversed


string `rvs`.

Step 6: If `s` is equal to `rvs`, go to Step 7. Otherwise,


go to Step 9.

Step 7: Print "String is palindrome".

Step 8: Go to Step 10.

Step 9: Print "String is not palindrome".

Step 10: End

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 1: Read the number of elements `n`.

Step 2: Read the array elements and store them in `a`.

Step 3: Read the element to be searched and store it in `p`.

Step 4: Initialize a variable `j` to 1.

Step 5: Use a while loop to search for the element in the


array:

Step 5.1: Check if `j` is less than `n` and if


`p` is not equal to `a[j]`.

Step 5.2: If true, increment `j`.

Step 6: If the loop terminates because `p` is found, go to


Step 7. Otherwise, go to Step 9.

Step 7: Print "$p is present at location $j".

Step 8: End.

Step 9: Print "$p is not present in the array".

Step 10: 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 1: Print "Enter the number of elements".

Step 2: Read the number of elements `n` from the user.

Step 3: Print "Enter the elements in the array".

Step 4: Create an array `a` of size `n`.

Step 5: Use a loop from `i = 0` to `i < n`.


Step 5.1: Read the `i`-th element from the user
and store it in the array `a`.

Step 6: Print "Numbers in the array are:" followed by the


elements of the array `a`.

Step 7: Use two nested loops:

Step 7.1: Outer loop from `i = 0` to `i < n`.

Step 7.1.1: Inner loop from `j = i` to `j <n`.

Step 7.1.1.1: Compare `a[i]` and `a[j]`.

Step 7.1.1.1.1: If `a[i]` is greater than


`a[j]`, swap their values.

Step 8: Print "The sorted numbers in ascending order are:"


followed by the elements of the array `a`.

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 1: Print "Enter a Range".

Step 2: Read the lower limit `m` and the upper limit `n`
from the user.

Step 3: Print "Prime Numbers between m and n :"

Step 4: Use a loop from `a = m` to `a <= n`.

Step 4.1: Initialize a variable `k` to 0.

Step 4.2: Use a loop from `i = 2` to `i < a`.

Step 4.2.1: Check if `a` is divisible by`i`

Step 4.2.1.1: If true, set `k` to 1 and


break out of the loop.

Step 4.3: If `k` is equal to 0, print the value


of `a` since it is a prime number.

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 1: Read the coefficients of the quadratic equation


from the user.

Step 2: Calculate the discriminant (D) using the formula:


D = b^2 - 4ac.

Step 3: Check the value of the discriminant (D) to


determine the nature of the roots:

Step 3.1: If D > 0, the equation has two distinct


real roots.

Step 3.2: If D = 0, the equation has one real


root (repeated).

Step 3.3: If D < 0, the equation has two complex


(imaginary) roots.

Step 4: Calculate the roots based on the nature of the


roots:

Step 4.1: If D > 0, calculate the roots using the


formula: x = (-b ± √D) / 2a.

Step 4.2: If D = 0, calculate the root using the


formula: x = -b / 2a.

Step 4.3: If D < 0, calculate the real and


imaginary parts of the roots:

Step 4.3.1: Real part = -b / 2a

Step 4.3.2: Imaginary part = √(-D) / 2a

Step 4.3.3: The two complex roots are: (-b /


2a) ± (√(-D) / 2a)i

Step 5: Output the calculated roots based on the nature of


the roots.

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 1: Read the number of rows 'rows' (should be odd) from


the user.

Step 2: Check if 'rows' is even (i.e., 'rows % 2 == 0').

Step 2.1: If 'rows' is even, print an error


message and exit the program.

Step 3: Calculate the middle row number and store it in


'mid_row': 'mid_row = (rows + 1) / 2'.

Step 4: Use a loop from 'i = 1' to 'mid_row' (inclusive) to


print the first half of the Pascal diamond.

Step 4.1: Use a loop from 'j = 1' to 'mid_row -


i' (inclusive) to print spaces.

Step 4.2: Use a loop from 'k = 1' to 'i'


(inclusive) to print asterisks '*' followed by a
space ' '.

Step 4.3: Print a new line to move to the next row.

Step 5: Use a loop from 'i = mid_row - 1' to 'i >= 1'
(inclusive) to print the second half of the Pascal diamond.

Step 5.1: Use a loop from 'j = 1' to 'mid_row -


i' (inclusive) to print spaces.

Step 5.2: Use a loop from 'k = 1' to 'i'


(inclusive) to print asterisks '*' followed by a
space ' '.

Step 5.3: Print a new line to move to the next row.

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 :

I would like to take this opportunity to extend my heartfelt


gratitude and appreciation to our esteemed computer science
teachers, Sourav sir and Abhishek sir, for their invaluable
guidance and support in completing my assignment on shell
scripting.
Throughout this assignment, their expertise and dedication
were evident as they patiently explained the concepts, shared
their profound knowledge, and clarified my doubts, which
greatly contributed to my understanding of shell scripting. Their
impact will undoubtedly resonate throughout my future
endeavours, and I am honoured to have had them as my
teachers.

...………………………………………
ARJYA SHARMA

30

You might also like