×

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the Armstrong number. Armstrong numbers include 0, 153, 1, 371, 407 and 270, for example.

Let's see if we can figure out why 407 is an Armstrong number:

407 = ( 4 * 4 * 4 ) + 0 + ( 7 * 7 * 7 )
Where ;
4 * 4 * 4 = 64
0 = 0 ;
7 * 7 * 7 = 343
64 + 343 = 407

Let us look at an example of a program in C++ to find Armstrong number:

// C++ program to determine whether the number is
// Armstrong number or not
#include < bits/stdc++.h >
#include < iostream >
#include < stdlib >
using namespace std ;    
/* Function to calculate x raised to the power y */
int power ( int a, unsigned int b )
{
	if (b == 0)
		return 1 ;
	if ( b % 2 == 0 )
		return power ( a , b / 2 ) * power( a , b / 2 ) ;
	return a * power ( a , b / 2 ) * power ( a , b / 2 ) ;
}
/* Function to calculate order of the number */
int order ( int a )
{
	int i = 0 ;
	while ( a ) {
		i++ ;
		a = a / 10 ;
	}
	return i ;
}
// Function to check whether the given number is
// Armstrong number or not
bool isArmstrong ( int a )
{
	// Calling order function
	int i = order ( a ) ;
	int temp = a , sum = 0 ;
	while ( temp ) {
		int y = temp % 10 ;
		sum += power ( y , i ) ;
		temp = temp / 10 ;
	}
	// If satisfies Armstrong condition
	return ( sum == a ) ;
}
// Driver Program
int main ( )
{
	int a = 153 ;
	cout << boolalpha << isArmstrong ( a ) << endl ;
	a = 1253 ;
	cout << boolalpha << isArmstrong ( a ) << endl ;
	return 0 ;
}

OUTPUT:

True 
False
………
Process executed in 0.12 seconds
Press any key to continue.

Another Example:

// C++ Program to find
// Nth Armstrong Number
#include < bits/stdc++.h >
#include < iostream >
#include < stdlib >
#include < math.h >
using namespace std ;


// Function to find Nth Armstrong Number
int NthArmstrong ( int a )
{
	int count = 0 ;


	// upper limit from integer
	for ( int i = 1 ; i < = INT_MAX ; i++ ) {
		int number = I , rem , digit = 0 , sum = 0 ;
		// Copy the value for num in number
		number = I ; 
		// Find total digits in number
		digit = ( int ) log10 ( number ) + 1 ; 
		// Calculate sum of power of digits
		while ( num > 0 ) {
			rem = number % 10 ;
			sum = sum + pow ( rem , digit ) ;
			number = number / 10 ;
		}
		// Check for Armstrong number
		if ( i == sum )
			count ++ ;
		if ( count == a )
			return I ;
	}
}
// Driver Function
int main ( )
{
	int a = 12 ;
	cout << NthArmstrong ( a ) ;
	return 0 ;
}

OUTPUT:

371

Let us see another example for checking Armstrong number for n digits:

#include < cmath >
#include < iostream >
#include < bits/stdc++.h >
using namespace std ; 
int main ( ) {
   int number , originalNumber , remainder, a = 0 , result = 0 , power ;
   cout << " enter an integer : " ;
   cin >> number ; 
   originalNumber = number ; 
   while ( originalNum != 0 ) {
      originalNumber /= 10 ;
      ++n ;
   }
   originalNumber = number ; 
   while ( originalNum != 0 ) {
      remainder = originalNum % 10 ; 
      // pow ( ) returns a double value
      // round ( ) returns the equivalent int
      power = round ( pow ( remainder , a ) ) ;
      result += power ;
      originalNumber /= 10 ;
   }
   if ( result == number )
      cout << number << " is an Armstrong number. " ;
   else
      cout << number << " is not an Armstrong number. " ;
   return 0 ;
}

OUTPUT:

Enter an integer: 9474
9472 is an Armstrong number.

The number of digits in the input number is computed first and saved in a in this application. In each iteration of the while loop, the pow ( ) function computes the power of specific numbers.


Related Topics

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Nullptr in C++

What is Nullptr in C++? A null pointer value is represented by the term nullptr. Use a null pointer value to indicate that a native pointer type, inner pointer, or object...

3 minutes read.

Armstrong Number using Do-While Loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

4 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

C++ operator

In this article, we will discuss about the operators in C++ with their types and examples. An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains...

5 minutes read.

How to Sort an Array in C++

What is Sorting? Sorting is a process of arranging elements in sequential order, either numerically or alphabetically. The sorting of a numerical array can be accomplished using a variety of algorithms,...

4 minutes read.

Decltype type Specifier in C++

The primary use of C++ decltype is to inspect the declaration type of an entity in an expression. The auto keyword can declare a particular type of variable, whereas the...

4 minutes read.

Copy constructor

Let's start by learning what a constructor is before diving into the copy constructor in C++. What is a constructor? A constructor is a particular type of class member function that configures the objects of...

7 minutes read.

10 Best C and C++ Books for Beginners & Advanced Programmers

If you want to become a skilled software developer, you should never stop learning, whether you're a working professional or a student. Why, therefore, only C or C++? The fundamental...

6 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Virtual base class in C++

Consider in a C++ program, there are 4 classes named class A, class B, class C, and class D. If class B and class c inherit properties from class A....

3 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than...

2 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Sliding Window Technique in C++

Sliding Window Technique or Window Sliding Technique is a computational technique that is mainly used to reduce the use of nested loop and replace it with a single loop. It...

4 minutes read.

Palindrome using For loop in C++

A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++ also allows...

6 minutes read.

C++ Program to Print Fibonacci Triangle

Fibonacci Triangle Program in CPP Definition: Fibonacci Triangle as the name suggests is the same as the Fibonacci number series where the next element is the sum of the previous two elements....

3 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes read.

Single Handling in C++

Introduction: Single handling in C++ refers to a technique for processing multiple events or requests with a single function or handler rather than creating separate functions for each task. This allows...

5 minutes read.