Here you will get program for armstrong number in C++.

A number whose sum of digits raised to power n is equal to itself is called armstrong number. Here n is the total digits in the number.

For example, 153 is armstrong number as 153 = 1^{3 }+ 5^{3 }+ 3^{3} = 1 + 125 +27.

## Program for Armstrong Number in C++

#include<iostream> #include<math.h> using namespace std; int main() { int n,m=0,p=0,x,y; cout<<"Enter any number: "; cin>>n; y=n; while(y!=0){ y=y/10; p++; } y=n; while(n!=0) { x=n%10; m+=pow(x,p); n=n/10; } if(y==m) cout<<"The given number is an armstrong number"; else cout<<"The given number is not an armstrong number"; return 0; }

**Output**

*Enter any number: 7*

*The given number is an armstrong number*

Kartik KumarThanks man your programs helps me a lot in my homework.

Neeraj MishraYour welcome bro, keep visiting!! 🙂

Shreetej Parkarthere is a fault as wen 1%10, x will become 10 and m will be +1000 rather than +1

Neeraj MishraThe logic is absolutely correct, 1%10 = 1. You are telling wrong logic. Just run the program, it is working fine.

Harshita DhingraSame problem!

This is not the right logic,you can check this by the no: 153

AdminThe logic is absolutely fine, I think there is some issue with codeblocks. When I run it in codeblocks it gives wrong output, but when I run it online the output is correct. Check this https://ideone.com/XElygr

marynis it also correct for any number more than 3 digits?

DeepeekaThank you

Himanshu BhardwajGood website easy for find out any sol.

shashineeraj can uhelp me doing vc++ programming

Jestinvery helpful and resourceful blog

Moon9Thanks. It did helped me.

Amit Khatri#include

#include

using namespace std;

int cnt(int num){

int c=0;

while(num!=0){

num=num/10;

c++;

}

return c;

}

int chkArm(int base,int pwr){

int result,temp;

while(base!=0){

temp = base%10;

cout << " temp is — " << temp << endl;

result+=pow(temp,pwr);

cout << "result is — "<<result << endl;

base=base/10;

}

return result;

}

int main()

{

int num,c,result;

cout <>num;

c=cnt(num);

cout << "\nYou entered " << c << " digits." <<endl;

result = chkArm(num,c);

cout << "Result is : " << result << endl;

}

Can some one please tell me that is there any problem in my code ? or if is it correct why its not giving the correct result?

raj kushwahSuperm program insimple way thankyou so much you help to us ..

HarshitNice and Easy man

Keep going man