Program in C++ to check a given number is Armstrong or not
1 #include<iostream>
2 #include<conio.h>
3 #include<math.h>
4 using namespace std;
5 int main()
6 {
7 int sum=0,r,n,a,as,digits=0;
8 cout<<"Enter a number:-";
9 cin>>n;
10 a=as=n;
11 //for count digits of a given number
12 while(n!=0)
13 {
14 n=n/10;
15 digits++;
16 }
17 while(a!=0)
18 {
19 r=a%10;
20 sum+=pow(r,digits);
21 a=a/10;
22 }
23 if(as==sum)
24 cout<<as<<" is Armstrong no.";
25 else
26 cout<<as<<" is not Armstrong no.";
27 getch();
28 }
Output:-
0 Comments