0
how we write a c+ program to check for armstrong or not
2 Answers
+ 1
/*********************************************************
find the no of digits
find the sum of power of digits of each digit in the num
compare the sum with original value
*********************************************************/
#include <iostream>
#include <math.h>
using namespace std;
int main (void ){
int x,r,s=0,t,c=0;
cout<<"Enter any number: "; cin>>x;
t = x;
//count digits
do{
x = x / 10;
c++;
}while (x != 0);
x = t;
do{
r = x % 10;
s = s + pow(r,c) ;
x = x / 10;
}while (x != 0);
if (t==s){
cout<<"Number is an armstrong";
}else{
cout<<"Number is not an armstrong";
}
}
0
/* the following program can check whether a number is an armstrong number */
#include <iostream>
using namespace std;
int main()
{
int number, sum = 0, temp, remainder;
cout<<"Enter an number"<<endl;
cin>>number;
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum ){
cout<<"Entered number is an armstrong number.";
}
else{
cout<<"Entered number is not an armstrong number.";
}
return 0;
}