BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to Check Armstrong Number using user-defined function

Last Updated: September 7, 2017 by Chaitanya Singh | Filed Under: C++ Programs

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.

Top Related Articles:

  1. C++ Program to check whether the input number is Even or Odd
  2. C++ Program to check Armstrong Number
  3. C++ Program to Check whether an input number is Prime or not
  4. C++ Program to Check Leap Year using function
  5. C++ Program to find the sum of n natural numbers

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap