Perl Programming-To Find Armstrong Number

0
255

If you are looking for a Pearl program to check whether a number is Armstrong or not, here is the right spot. Let us start with the algorithm first.

Algorithm:

1. Start

2. Read the number to n

3. Initialize sum=0 and temp=n

4. While temp not equal to zero, do the following steps:

5. Calculate rem as temp%10. Now calculate the value of sum as “$sum+(rem*rem*rem)

6. Now omit the last digit of the number by temp/10 and assign the value to temp

7. If the sum is equal to n, then echo n is “Armstrong number” or else echo “Not Armstrong”

8. Stop

Perl program for armstrong number

print “Enter the number”;

$n=<>;

$t=$n;

$s=0;

while ($n > 0)

{

$r=$n%10;

$i=$r*$r*$r;

$s=$s+$i;

$n=$n/10;

}

if($s == $t)

{

print “Armstrong Number”;

}

else

{

print “Not Armstrong”;

}

OUTPUT

Enter the number

153

Armstrong Number

LEAVE A REPLY