Armstrong Number

Posted by admin at July 6, 2021

The Armstrong number definition is the number in any given number base, which forms the total of the same number, when each of its digits is raised to the power of the number of digits in the number.

For example, using a simple number 153 and the decimal system, we see there are 3 digits in it. If we do a simple mathematical operation of raising each of its digits to the power of 3, and then totalling the sum obtained, we get 153. That is 1 to the power of 3 5 to the power of 3 3 to the power of three is 1 125 27 153. This can also be represented as 1^3 5^3 3^3=153. The number 153 is an example of the Armstrong number which also has a unique property that one can use any number system.

Steps to Find Armstrong Number:

1. The number of digits in number.

 2. The sum of digits of a number or individual digit sums are got by performing number mod 10, where mod is called the remainder operation.

 3. The individual digit is then raised to the power (number of digits) and stored.

 4. The number is then divided by 10 in order to obtain the second digit.

 5. All the above 3-steps numbered Steps 2, 3 and 4 are performed until the value of number is greater than 0.

 6. When the number is less than 0, end the while loop.

 7. Check the sum obtained or Armstrong value is the same as the original number

 8. When yes, the number is labelled an Armstrong number

C Peogram:

#include<stdio.h>  
 int main()    
{    
int n,r,sum=0,temp;    
printf("enter the number=");    
scanf("%d",&n);    
temp=n;    
while(n>0)    
{    
r=n%10;    
sum=sum+(r*r*r);    
n=n/10;    
}    
if(temp==sum)    
printf("armstrong  number ");    
else    
printf("not armstrong number");    
return 0;  
} 

Python Program:

num= int(input("Enter a number: "))
num_original =num2=num
sum1 = 0
cnt=0
while(num>0):
               cnt=cnt 1
               num=num//10
while num2>0:
   rem = num2% 10
   sum1  = rem ** cnt
   num2//= 10
if(num_original==sum1):
    print("Armstrong!!")
else:
    print("Not Armstrong!")

JavaScript Program:


 const number = prompt("Enter a positive integer");
 const numberOfDigits = number.length;
 let sum = 0;

 let temp = number;
 while (temp > 0) {
      let remainder = temp % 10;
      sum += remainder ** numberOfDigits;
      temp = parseInt(temp / 10);   
 
 }
 if (sum == number) {
    console.log("Number is Armstrong");
 }
else{
console.log("Number is not Armstrong");

}

Comments

Write a Reply or Comment