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: -
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
- //Armstrong numbers upto given number
- #include<iostream>
- #include <math.h>
- using namespace std;
- bool isArmstrong(double n)
- {
- int temp,digits=0,last=0;
- double sum=0;
- temp=n;
- while(temp>0)
- {
- temp /= 10;
- digits++;
- }
- temp = n;
- while(temp>0)
- {
- last = temp%10;
- sum += pow(last,digits);
- temp /= 10;
- }
- if(n==sum) return true;
- else return false;
- }
- int main()
- {
- double num,count=0;
- cout<<"Enter any number: ";
- cin>>num;
- cout<<"Armstrong numbers upto "<<num<<" are: ";
- for(double i=0; i<=num; i++)
- if(isArmstrong(i)) cout<<i<<" ";
- }
The output will be as:
- Enter any number: 999
- 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
Post a Comment