What are the Armstrong Numbers between 1 to 999?

 Armstrong/Narcissistic Number: If a number is equal to sum of each of its digits raised to power of its digits, it is said to be an Armstrong number or a Narcissistic number.

Ex: -

371=33+73+13=27+343+1=371

1634=14+64+34+44=1+1296+81+256=1634

Why is 0 excluded in the question? 0 is also an armstrong number. Here’s a C++ program to find all the Armstrong numbers upto any given number

  1. //Armstrong numbers upto given number 
  2. #include<iostream> 
  3. #include <math.h> 
  4. using namespace std; 
  5.  
  6. bool isArmstrong(double n) 
  7. { 
  8. int temp,digits=0,last=0; 
  9. double sum=0; 
  10. temp=n; 
  11.  
  12. while(temp>0)  
  13. { 
  14. temp /= 10; 
  15. digits++; 
  16. } 
  17.  
  18. temp = n; 
  19.  
  20. while(temp>0) 
  21. { 
  22. last = temp%10; 
  23. sum += pow(last,digits); 
  24. temp /= 10; 
  25. } 
  26.  
  27. if(n==sum) return true;  
  28. else return false; 
  29. } 
  30.  
  31. int main()  
  32. {  
  33. double num,count=0; 
  34.  
  35. cout<<"Enter any number: "; 
  36. cin>>num; 
  37. cout<<"Armstrong numbers upto "<<num<<" are: "; 
  38.  
  39. for(double i=0; i<=num; i++) 
  40. if(isArmstrong(i)) cout<<i<<" "; 
  41. } 

The output will be as:

  1. Enter any number: 999 
  2. Armstrong numbers upto 999 are: 0 1 2 3 4 5 6 7 8 9 153 370 371 407 

Armstrong numbers are very rare and hard to find out when the number of digits increase as you can see in the table below.

Image Source: Excel Sheet I’ve prepared.

Edit : Half of the answers to this question are wrong here. Because they don’t acknowledge 1 to 9 as Armstrong numbers. An Armstrong number should be equal to sum of digits raised to the number of digits (not their cubes). You can check the definition of Armstrong number again if you want to.

post: https://qr.ae/prq2iM

Comments

Popular posts from this blog

Whiteboarding Interviews

Version Control with Git: A Beginner’s Guide

Enhancing Internet Speed Through CMD Commands