If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
Write a program that prints all Armstrong numbers from 1-1000.
Solution:
OUTPUT: For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
Write a program that prints all Armstrong numbers from 1-1000.
Solution:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<conio.h> | |
using namespace std; | |
int main() | |
{ | |
int rem,res,div; | |
for(int i = 1; i<=1000; i++) | |
{ | |
rem = i%10; | |
div = i/10; | |
res = rem*rem*rem; | |
if(div>=1) | |
{ | |
rem = div%10; | |
div = div/10; | |
res = res + rem*rem*rem; | |
if(div>=1) | |
{ | |
rem = div%10; | |
div = div/10; | |
res = res + rem*rem*rem; | |
} | |
} | |
if(res == i) | |
cout<<res<<endl; | |
} | |
getch(); | |
return 0; | |
} |
No comments:
Post a Comment