#!/bin/bash
# SCRIPT: armstrongnumber.sh
# PURPOSE: Check if the given number is Armstrong number ?
#
# Armstrong numbers are the sum of their own digits to the power of the number of digits.
# As that is a slightly brief wording, let me give an example:
# 153 = 1³ + 5³ + 3³
# Each digit is raised to the power three because 153 has three digits.
# They are totalled and we get the original number again!
# Notice that Armstrong numbers are base dependent,but we'll mainly be
# dealing with base 10 examples.The Armstrong numbers up to 5 digits
# are 1 to 9,153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727,93084
#
#####################################################################
# Script Starts Here #
#####################################################################
echo "Enter any Number to Check if entered Number is Armstrong or Not : "
read num
num1=$num
original=$num
armnum=0
count=0
mul=1
count1=0
while [ ! $num1 -eq 0 ] # Get Length of Number
do
rem=`expr $num1 % 10`
count=`expr $count + 1`
num1=`expr $num1 / 10`
done
count1=$count
while [ ! $num -eq 0 ]
do
rem=`expr $num % 10`
while [ ! $count1 -eq 0 ]
do
mul="$(($mul * $rem))"
count1=`expr $count1 - 1`
done
armnum=`expr $armnum + $mul`
num=`expr $num / 10`
count1=$count
mul=1
done
if [ $original -eq $armnum ]
then
echo "$original is An Armstrong Number."
else
echo "$original is Not An Armstrong Number."
fi
No comments:
Post a Comment