how we write a c+ program to check for armstrong or not | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how we write a c+ program to check for armstrong or not

23rd Oct 2016, 12:01 PM
kamal
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"; } }
13th Feb 2017, 3:48 PM
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender)
เคฆเฅ‡เคตเฅ‡เค‚เคฆเฅเคฐ เคฎเคนเคพเคœเคจ (Devender) - avatar
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; }
23rd Oct 2016, 1:37 PM
liwei
liwei  - avatar