×

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

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.

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.

Hexadecimal to Decimal in C++

In computers, hexadecimal numbers are represented with base 16 and decimal numbers are represented with base 10 and values 0-9, whereas hexadecimal numbers have digits ranging from 0 to 15,...

3 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

C++ find missing in the second array

Given two arrays A and B of sizes n and m. Find the elements from array A that are not present in array B. Example Input : a[] = {1, 2, 3, 5,...

3 minutes read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

Ascending order in C++

In C++, the term "ascending order" refers to a specific order in which a list of elements is arranged. When a list of elements is arranged in ascending order, the...

3 minutes read.

Delete Operator in C++

Overview We can reserve allocation for a variable or an array at runtime in C++. Dynamic memory allocation is the term for this. After utilising a variable in C++, we must...

4 minutes read.

Random Number Generator in C++

In programming, we need to frequently create the randomly. For example, a dice game, handing out cards to players, apps for rearranging tunes, etc. T There are two tools available in...

4 minutes read.

While Loop Examples in C++

While Loop A while loop repeats all code in its body, also known as a while statement, as long as a specific condition is satisfied. The loop ends if or when...

4 minutes read.

Object in C++

In this article, we will learn about Object in C++. In short, an object is a stateful entity with behaviour. Data is referred to as state, and functionality is referred to...

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

Constructor Overloading in C++

A Constructor is a class member function that is used to initialize the class's objects. Constructors have no return type and are called automatically when an object is formed. Constructor Characteristics Constructors...

3 minutes read.

C++ Namespaces

An Overview In each scope, a name can only represent one entity. As a result, there cannot be two independent variables with the similar names in the same scope, as this may cause...

10 minutes read.

Virtual Functions and Runtime Polymorphism in C++

In this tutorial, we will explore more on virtual functions and runtime polymorphism in the most useful language C++. A virtual function is a member function with the keyword virtual used...

6 minutes read.

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

INT_MAX and INT_MIN in C/C++

In competitive programming, assigning a variable that maximum or minimum value a data type can carry is frequently necessary. Still, it might be challenging to recall such a significant, exact...

3 minutes read.

Priority Queue in C++

Priority Queue in C++ Introduction: We have already come across Queues by concluding that they are linear data structures that follow FIFO (First-In-First-Out) approach. We had also discussed the syntax of queues...

4 minutes read.

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++. This same process, due to a sorted array, is to erase the redundant components from the array. Examples: Input  : arr[] = {2, 2, 2,...

4 minutes read.

C++ File Handling

File handling is a mechanism that manipulates the data stored in files. File handling store output data from the program to external file and read file data to the program. There...

3 minutes read.