beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

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

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.

Leave a Reply Cancel reply

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

Programs

  • C Programs
  • Java Programs

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap