Showing posts with label program for printing armstrong numbers upto inputted digit. Show all posts
Showing posts with label program for printing armstrong numbers upto inputted digit. Show all posts

Saturday, April 23, 2022

printing armstrongs upto n

 #include <iostream>

using namespace std;
#include <cmath>
#include <conio.h>

class arm
{
    int num1;

public:
    int sum, rem, i, num, count;
    arm(int num1)
    {
        this->num1 = num1;
    }
    void check()
    {
        if (num1 > 0)
        {
            for (i = 0; i <= num1; i++)
            {

                // count gives the number of digits in i
                count = 0;

                // store value of i in num
                num = i;

                // count the number of digits in num and i
                while (num > 0)
                {
                    ++count;
                    num /= 10;
                }

                // initialize sum to 0
                sum = 0;

                // store i in num again
                num = i;

                // get sum of power of all digits of i
                while (num > 0)
                {
                    rem = num % 10;
                    sum = sum + pow(rem, count);
                    num /= 10;
                }

                // if sum is equal to i, then it is Armstrong
                if (sum == i)
                {
                    cout << i << "\t ";
                }
            }
        }
        else
            cout << "number entered is less than 1\n";
    }
};
int main()
{
    system("cls");
    int num1;
    cout << "enter number upto which you want armstongs\n";
    cin >> num1;
    arm a(num1);
    system("cls");
    cout << "armstongs upto " << num1 << endl;
    a.check();

    return 0;
}

HackCache ’26: A 48-Hour Online Hackathon Open to Every Student

  Hackathons are often associated with computer science students, experienced developers, and teams that already know exactly what they are...

Popular Posts