An integer number is called Armstrong number if sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3*3*3 + 7*7*7 + 1*1*1 = 371.
Lets write a program to check whether the input number is armstrong number using user-defined function. If you are looking for a program to check armstrong number using loop then see: C++ Program to check Armstrong number using for loop.
Example: Check whether input number is Armstrong Number or not
To understand this program you should have the knowledge of if-else statement, while loop and user-defined function.
#include <iostream>
using namespace std;
bool checkArmstrongNumber(int num);
int main(){
int num;
bool flag;
cout<<"Enter a positive integer: ";
cin>>num;
//Calling function
flag = checkArmstrongNumber(num);
if(flag == true)
cout<<num<<" is an Armstrong number.";
else
cout<<num<<" is not an Armstrong number.";
return 0;
}
/* User defined function that checks whether the passed
* integer number is Armstrong or not
*/
bool checkArmstrongNumber(int num) {
int temp, sum=0, digit;
bool isArm;
temp = num;
while(temp != 0) {
digit = temp % 10;
sum = sum +(digit * digit * digit);
temp = temp/10;
}
if (sum==num)
isArm = true;
else
isArm = false;
return isArm;
}
Output:
Enter a positive integer: 371 371 is an Armstrong number.
Leave a Reply