0% found this document useful (0 votes)
15 views7 pages

1. Configuring Cron (1)

fsdgbsd ghwr wrg wgb
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)
15 views7 pages

1. Configuring Cron (1)

fsdgbsd ghwr wrg wgb
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/ 7

1.

Shell Script to Find GCD of Two Positive Numbers

#!/bin/bash

# Function to calculate GCD

gcd() {

a=$1

b=$2

while [ $b -ne 0 ]; do

temp=$b

b=$((a % b))

a=$temp

done

echo "GCD is: $a"

# Read two positive numbers

read -p "Enter first positive number: " num1

read -p "Enter second positive number: " num2

gcd $num1 $num2

outuput :
2. Shell Script to Find Sum of All Even Numbers Between 1 to 50

#!/bin/bash

sum=0

# Loop through numbers from 1 to 50

for ((i=1; i<=50; i++)); do

if (( i % 2 == 0 )); then

sum=$((sum + i))

fi

done

echo "Sum of all even numbers between 1 to 50 is: $sum"

output :
3. Shell Script to Find Sum of Digits.

#!/bin/bash

# Function to calculate the sum of digits

sum_of_digits() {

num=$1

sum=0

while [ $num -gt 0 ]; do

digit=$((num % 10))

sum=$((sum + digit))

num=$((num / 10))

done

echo "Sum of digits is: $sum"

# Read a positive number


read -p "Enter a positive number: " number

sum_of_digits $number

output :

4. Shell Script to Find Reverse of Any Positive Number.

#!/bin/bash

# Function to reverse a number

reverse_number() {

num=$1

reverse=0

while [ $num -gt 0 ]; do

digit=$((num % 10))
reverse=$((reverse * 10 + digit))

num=$((num / 10))

done

echo "Reverse of the number is: $reverse"

# Read a positive number

read -p "Enter a positive number: " number

reverse_number $number

output :

5 Shell Script to Find Factorial of Any Positive Number.

#!/bin/bash
# Function to calculate factorial

factorial() {

num=$1

fact=1

for ((i=1; i<=num; i++)); do

fact=$((fact * i))

done

echo "Factorial of $num is: $fact"

# Read a positive number

read -p "Enter a positive number: " number

factorial $number

output :

6. Write a shell script to draw a five star pattern.


#!/bin/bash

# Function to print five-star pattern

draw_star_pattern() {

for ((i=1; i<=5; i++)); do

for ((j=1; j<=i; j++)); do

echo -n "* "

done

echo

done

draw_star_pattern

output :

You might also like