0% found this document useful (0 votes)
17 views4 pages

Linux Scripting B23cse035

Uploaded by

Harsh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Linux Scripting B23cse035

Uploaded by

Harsh Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA LAB TASK-1

Name: Harsh Shah


Class: B.Tech Sem-3
Division: B
Roll No: 35
Enrollment number: 202302626010115

1) Write a shell script to check entered string is


palindrome or not.
#!/bin/bash

# Read the number from the user


read -p "Enter a number: " number

# Initialize product to 1
product=1

# Convert the number to an array of digits and multiply each digit


while [ $number -gt 0 ]
do
digit=$((number % 10))
product=$((product * digit))
number=$((number / 10))
done

# Output the result


echo "Product of the digits is: $product"
2) Write a shell script to check entered string is armstrong
or not.
#!/bin/bash

# Read input from the user


echo "Enter a number:"
read input

# Check if the input is a valid number


if ! [[ "$input" =~ ^[0-9]+$ ]]; then
echo "Invalid input. Please enter a numeric value."
exit 1
fi

num=$input
length=${#num}
sum=0
temp=$num

while [ $temp -gt 0 ]; do


digit=$((temp % 10))
temp=$((temp / 10))
sum=$((sum + digit ** length))
done

if [ $sum -eq $num ]; then


echo "$num is an Armstrong number."
else
echo "$num is not an Armstrong number."
fi
3) Write a shell script to Find sum of first n odd numbers.
#!/bin/bash

# Read the value of n from the user


read -p "Enter the value of n: " n

# Initialize variables
sum=0
odd=1

# Calculate the sum of first n odd numbers


for (( i=1; i<=n; i++ ))
do
sum=$((sum + odd))
odd=$((odd + 2))
done

# Output the result


echo "Sum of first $n odd numbers is: $sum"
4) Write a shell script to find multiplication of digits 123.
#!/bin/bash

# Read the number from the user


read -p "Enter a number: " number

# Initialize product to 1
product=1

# Convert the number to an array of digits and multiply each digit


while [ $number -gt 0 ]
do
digit=$((number % 10))
product=$((product * digit))
number=$((number / 10))
done

# Output the result


echo "Product of the digits is: $product"

You might also like