0% found this document useful (0 votes)
24 views5 pages

Assignment 4

Uploaded by

sreejeet17111995
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)
24 views5 pages

Assignment 4

Uploaded by

sreejeet17111995
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

Assignment 4

1. Write a shell program to print number using ‘while’ loop.

Script:

echo "Enter a number"


read n
m=0
echo "Number are as below :"
while((n>=0))
do
echo "$m"
((m++))
((n--))
done

2. Write a shell program to print number using ‘for’ loop.

Script:
echo "Enter a number"
read n
echo "Number are as below :"
for((i=0;i<=$n;i++))
{
echo $i
}
3. Write a shell program to find factorial of given number using
while loop.
Script:

echo "Enter a number"


read n
fact=1
while (($n>1))
do
fact=$(( $fact * $n ))
((n--))
done
echo "Factorial of entered number = $fact
4. Write a shell program to find factorial of given number using
for loop.

Script:
echo "Enter a number"
read n
fact=1
for((i=1;i<=$n;i++))
#for((i=$n;i>1;i--))
{
fact=$((fact*i))
}
echo "Result of $n! = $fact"

5. Write a shell program to find a reverse of a number using while


loop.
Script:
echo "Enter a number:"
read n
num=n
rem=0
sum=0
while(($num!=0))
do
rem=$((num%10))
num=$((num/10))
sum=$((sum*10+rem))
done
echo "The reverse number of $n is : $sum"
6. Write a shell program to find a given number is palindrome
number or not using while loop.
Script:

echo "Enter a number:"


read n
num=n
rem=0
sum=0
while(($num!=0))
do
rem=$((num%10))
num=$((num/10))
sum=$((sum*10+rem))
done
if (($sum==$n))
then
echo "$n is a palindrome number"
else
echo "$n is not a palindrome number"
fi
7. Write a shell program to find a given number is armstrong
number or not using while loop.

Script:
echo "Enter a number:"
read n
num=n
rem=0
sum=0
while(($num>0))
do
rem=$((num%10))
num=$((num/10))
sum=$((sum+rem*rem*rem))
done
if(($sum==n))
then
echo "Armstrong number"
else
echo "Not Armstrong number"
fi

You might also like