C++

Using A User-Defined Function, A C++ Program Checks The Armstrong Number

If the total of the cubes of an integer number’s digits equals the number itself, the number is said to be an Armstrong number. Since 3*3*3 + 7*7*7 + 1*1*1 = 371, for instance, 371 is an Armstrong number.

Let’s create a program that uses a user-defined function to determine whether the input number is an armstrong number. See C++ Program to verify Armstrong number using for loop if you’re seeking for a program to check Armstrong number using a loop.

Check the input number to see if it’s an Armstrong number, for instance.

You need be familiar with the if-else statement, while loop, and user-defined function in order to comprehend this program.

#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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button