Armstrong Number in C++ using For loop | Function | Class | Recursion
Armstrong number in C++ using For loop, Function, Class, and Recursion.
In this article, you will learn how to check Armstrong number in C++ using for loop, function, class, and recursion.
Example-1
Example-2
Armstrong Number
A positive integer is called an Armstrong number (of order n) if
You should have knowledge of the following topics in c++ programming to understand these programs:
- C++ cmath library
- C++
main()
function - C++
for
loop statement - C++
while
loop statement - C++
if-else
condition statement - C++
cin
object - C++
cout
object
In this article, we solve this problem in six methods:
- Using the For loop
- Using the Function
- Using the Class and Object
- Using the Recursion
- Using the While loop
- Without using the pow() function
Armstrong Number in C++ using For loop
// Armstrong Number in C++ using For loop
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x, y, rem, n = 0;
float r = 0.0;
// x - To store the input number
// y - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
cout << "-----Enter the integer number-----\n";
cin >> x;
y = x;
// store the number of digits of x in n
for (y = x; y != 0; ++n) {
y /= 10;
}
for (y = x; y != 0; y /= 10) {
rem = y % 10;
// store the sum of the power of individual digits in r
r += pow(rem, n);
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
cout << x << " is an Armstrong number.\n";
} else {
cout << x << " is not an Armstrong number.\n";
}
return 0;
}
Output
-----Enter the integer number-----
407
407 is an Armstrong number.
Armstrong Number in C++ using Function
// Armstrong Number in C++ using Function
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
// This function will check input number is Armstrong or not
void CheckArmstrong(int p) {
int q, rem, n = 0;
float r = 0.0;
// q - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
q = p;
// store the number of digits of x in n
for (q = p; q != 0; ++n) {
q /= 10;
}
for (q = p; q != 0; q /= 10) {
rem = q % 10;
// store the sum of the power of individual digits in r
r += pow(rem, n);
}
// if "p" is equal to "r", the number is an Armstrong number
if ((int) r == p) {
cout << p << " is an Armstrong number.\n";
} else {
cout << p << " is not an Armstrong number.\n";
}
}
// It's the driver function
int main() {
int x; // To store the input number
cout << "-----Enter the integer number-----\n";
cin >> x;
// It will produce the final output
CheckArmstrong(x);
return 0;
}
Explanation
In this given program, we have taken input 407
a positive integer number to check if it is an Armstrong or not using the for loop and pow() function.
Then, we applied the Armstrong number formula in these loops to check the sum of its digits' multiplication with its length equal to the given input value.
Then It will return the final output of the program whether the input number is Armstrong or Not.
Armstrong Number in C++ using Class and Object
// Armstrong Number in C++ using Class and Object
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
class Armstrong {
public:
int p;
float r = 0.00;
// This method will check input number is Armstrong or not
void CheckArmstrong(int x)
{
p = x;
int q, rem, n = 0;
// q - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
q = p;
// store the number of digits of x in n
for (q = p; q != 0; ++n) {
q /= 10;
}
for (q = p; q != 0; q /= 10) {
rem = q % 10;
// store the sum of the power of individual digits in r
r += pow(rem, n);
}
}
// This method will print the final output of the program
void ArmstrongNumber()
{
// if "p" is equal to "r", the number is an Armstrong number
if ((int) r == p)
cout << p << " is an Armstrong number.\n";
else
cout << p << " is not an Armstrong number.\n";
}
};
// It's the driver function
int main() {
int x; // To store the input number
cout << "-----Enter the integer number-----\n";
cin >> x;
// It will create the instance of the `Armstrong` class
Armstrong AM;
// It will produce the final output
AM.CheckArmstrong(x);
AM.ArmstrongNumber();
return 0;
}
Output
-----Enter the integer number-----
311
311 is not an Armstrong number.
Armstrong Number in C++ using Recursion
// Armstrong Number in C++ using Recursion
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
// This function will check input number is Armstrong or not
// This will work recursively
int CheckArmstrong(int p) {
if (p > 0)
return (pow(p %10, 3) + CheckArmstrong(p / 10));
}
// It's the driver function
int main() {
int x; // To store the input number
cout << "-----Enter the integer number-----\n";
cin >> x;
// It will produce the final output
if ((int) CheckArmstrong(x) == x)
cout << x << " is an Armstrong number.\n";
else
cout << x << " is not an Armstrong number.\n";
return 0;
}
C++ Program to Check Armstrong Number using While loop
// C++ Program to Check Armstrong Number using While loop
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x, y, z, rem, n = 0;
float r = 0.0;
// x - To store the input number
// y & z - To store the storage of original input number to check
// rem - To store the reminder
// r - To store the result variable
cout << "-----Enter the integer number-----\n";
cin >> x;
z = y = x;
while (y != 0) {
y /= 10;
++n;
}
while (z != 0) {
rem = z % 10;
r += pow(rem, n);
z /= 10;
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
cout << x << " is an Armstrong number.\n";
} else {
cout << x << " is not an Armstrong number.\n";
}
return 0;
}
Output
-----Enter the integer number-----
407
407 is an Armstrong number.
C++ Program to Check Armstrong Number without using pow() function
// C++ Program to Check Armstrong Number without using pow() function
#include <iostream>
using namespace std;
int main() {
int x, y, z, rem = 1, rem2 = 1, n = 0, m = 0;
float r = 0.00;
// x - To store the input number
// y & z - To store the storage of original input number to check
// rem & rem2 - To store the reminder
// r - To store the result variable
cout << "-----Enter the integer number-----\n";
cin >> x;
z = y = x;
while (y != 0) {
y /= 10;
++n;
}
m = n;
while (z != 0) {
rem = z % 10;
rem2 = 1;
while (n > 0) {
rem2 *= rem;
--n;
}
n = m;
r += rem2;
z /= 10;
}
// if x is equal to r, the number is an Armstrong number
if ((int) r == x) {
cout << x << " is an Armstrong number.\n";
} else {
cout << x << " is not an Armstrong number.\n";
}
return 0;
}
Output
-----Enter the integer number-----
407
407 is an Armstrong number.
Also, visit these links
C Program to Enter Two Numbers and Perform All Arithmetic Operations