Finding Armstrong Number using Destructor in C++ || Destructor || C++
In this, we are going to see how to see if a given number is  Armstrong Number or not using Destructor, in C++.
 


The Code given below can be used in TURBO C++ Compilers: -

#include <iostream.h>
#include<conio.h>

class Armstrong
{
private:
    int num, r, sum, temp;

public:
    Armstrong(int num)
    {
        temp = num;
        sum = 0;
        while (num > 0)
        {
            r = num % 10;
            sum = sum + (r * r * r);
            num = num / 10;
        }
    }
    int display()
    {
        if (temp == sum)
        {
            cout << "\nGiven number is ARMSTRONG NUMBER.\n";
        }
        else
        {
            cout << "\nGiven number is NOT an ARMSTRONG NUMBER.\n";
        }
    }
    ~Armstrong()
    {
        cout << "DESTRUCTOR is called.";
    }
};

void main()
{
    clrscr();
    int number;
    cout << "Enter a number:-\n";
    cin >> number;
    Armstrong am(number);
    am.display();
    getch();
}
//.........Coded by YASH ALAPURIA
 


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;

class Armstrong
{
private:
    int num, r, sum, temp;

public:
    Armstrong(int num)
    {
        temp = num;
        sum = 0;
        while (num > 0)
        {
            r = num % 10;
            sum = sum + (r * r * r);
            num = num / 10;
        }
    }
    int display()
    {
        if (temp == sum)
        {
            cout << "\nGiven number is ARMSTRONG NUMBER.\n";
        }
        else
        {
            cout << "\nGiven number is NOT an ARMSTRONG NUMBER.\n";
        }
    }
    ~Armstrong()
    {
        cout << "DESTRUCTOR is called.";
    }
};

int main()
{
    system("cls");
    int number;
    cout << "Enter a number:-\n";
    cin >> number;
    Armstrong am(number);
    am.display();
    return 0;
}
//.........Coded by YASH ALAPURIA
    


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post