0% found this document useful (0 votes)
4 views

Shell_Script_Programs_Algorithms

The document provides a collection of shell script programs along with their algorithms for various tasks such as determining if a number is odd or even, checking for prime numbers, summing two numbers, and calculating the factorial of a number. Each program includes a brief algorithm followed by the corresponding shell script code. The tasks cover basic mathematical operations and number classifications.
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)
4 views

Shell_Script_Programs_Algorithms

The document provides a collection of shell script programs along with their algorithms for various tasks such as determining if a number is odd or even, checking for prime numbers, summing two numbers, and calculating the factorial of a number. Each program includes a brief algorithm followed by the corresponding shell script code. The tasks cover basic mathematical operations and number classifications.
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/ 5

Shell Script Programs - Algorithms and Codes

1) Odd or Even
Algorithm:
1. Start.
2. Input a number.
3. If number % 2 == 0, print 'Even'; else print 'Odd'.
4. Stop.

Shell Script:
#!/bin/bash
echo "Enter a number"
read num
if [ $(($num % 2)) -eq 0 ]
then
echo "$num is even."
else
echo "$num is odd."
fi

2) Prime or Not
Algorithm:
1. Start.
2. Input number.
3. If num < 2, print 'Not Prime'.
4. Loop i=2 to sqrt(num): if num % i == 0, print 'Not Prime' and exit. Else, print 'Prime'.
5. Stop.

Shell Script:
#!/bin/bash
echo "Enter a number"
read num
if [ $num -lt 2 ]
then
echo "$num is not a prime number."
exit 0
fi
for (( i=2; i*i<=num; i++ ))
do
if [ $(($num % i)) -eq 0 ]
then
echo "$num is not a prime number."
exit 0
fi
done
echo "$num is a prime number."

3) Sum of Two Numbers


Algorithm:
1. Start.
2. Input two numbers.
3. Sum = num1 + num2.
4. Print Sum.
5. Stop.

Shell Script:
#!/bin/bash
echo "Enter 1st number"
read num1
echo "Enter 2nd number"
read num2
sum=$(($num1 + $num2))
echo "The sum of $num1 and $num2 is $sum."

4) Sum of Digits of a Number


Algorithm:
1. Start.
2. Input number.
3. While num != 0: digit = num % 10, sum += digit, num /= 10.
4. Print sum.
5. Stop.

Shell Script:
#!/bin/bash
echo "Enter a number"
read num
sum=0
temp=$num
while [ $temp -ne 0 ]
do
digit=$(($temp % 10))
sum=$(($sum + $digit))
temp=$(($temp / 10))
done
echo "The sum of digits is $sum."

5) Positive, Negative or Zero


Algorithm:
1. Start.
2. Input number.
3. If num > 0, print 'Positive'.
4. Else if num < 0, print 'Negative'.
5. Else print 'Zero'.
6. Stop.

Shell Script:
#!/bin/bash
echo "Enter a number"
read num
if [ $num -gt 0 ]
then
echo "$num is positive."
elif [ $num -lt 0 ]
then
echo "$num is negative."
else
echo "The number is zero."
fi

6) Divisible by 2, 3 or 5
Algorithm:
1. Start.
2. Input number.
3. Check divisibility for 2, 3, 5 and print accordingly.
4. Stop.

Shell Script:
#!/bin/bash
echo "Enter a number"
read num
if [ $(($num % 2)) -eq 0 ]; then echo "$num is divisible by 2."; fi
if [ $(($num % 3)) -eq 0 ]; then echo "$num is divisible by 3."; fi
if [ $(($num % 5)) -eq 0 ]; then echo "$num is divisible by 5."; fi

7) Number Range Check (1-10, 11-20, 21-30)


Algorithm:
1. Start.
2. Input number.
3. Check range and print appropriate message.
4. Stop.

Shell Script:
#!/bin/bash
echo "Enter a number"
read num
if [ $num -ge 1 ] && [ $num -le 10 ]
then
echo "$num is between 1 and 10."
elif [ $num -ge 11 ] && [ $num -le 20 ]
then
echo "$num is between 11 and 20."
elif [ $num -ge 21 ] && [ $num -le 30 ]
then
echo "$num is between 21 and 30."
else
echo "$num is not between 1 and 30."
fi

8) Sum of N Numbers
Algorithm:
1. Start.
2. Input N.
3. Sum = 0.
4. Loop i=1 to N: sum += i.
5. Print sum.
6. Stop.

Shell Script:
#!/bin/bash
echo "Enter the value of N"
read n
sum=0
for (( i=1; i<=n; i++ ))
do
sum=$(($sum + $i))
done
echo "The sum of first $n numbers is $sum."

9) Sum of First 10 Numbers


Algorithm:
1. Start.
2. Sum = 0.
3. Loop i=1 to 10: sum += i.
4. Print sum.
5. Stop.

Shell Script:
#!/bin/bash
sum=0
for (( i=1; i<=10; i++ ))
do
sum=$(($sum + $i))
done
echo "The sum of first 10 numbers is $sum."

10) Factorial of a Number


Algorithm:
1. Start.
2. Input number.
3. fact = 1.
4. Loop i=1 to n: fact *= i.
5. Print fact.
6. Stop.

Shell Script:
#!/bin/bash
echo "Enter a number"
read num
fact=1
for (( i=1; i<=num; i++ ))
do
fact=$(($fact * $i))
done
echo "The factorial of $num is $fact."

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy