answersLogoWhite

0

#include<iostream>

#include<vector>

unsigned count_digits (unsigned num, const unsigned base=10)

{

unsigned count=1;

while (num/=base) ++count;

return count;

}

class number

{

std::vector<unsigned> value;

unsigned base;

public:

number (const unsigned _value, const unsigned _base=10): value {}, base {_base} { *this = _value; }

number& operator= (const unsigned _value);

operator unsigned () const;

bool is_narcissistic () const;

};

number& number::operator= (unsigned _value)

{

unsigned count = count_digits (_value, base);

value.resize (count);

while (count)

{

value[value.size()-count--] = _value%base;

_value/=base;

}

return *this;

}

number::operator unsigned () const

{

unsigned num = 0;

for (unsigned index=0; index<value.size(); ++index)

num += value[index]*static_cast<unsigned>(std::pow (base, index));

return num;

}

bool number::is_narcissistic () const

{

unsigned num = 0;

for (unsigned index=0; index<value.size(); ++index)

num += static_cast<unsigned>(std::pow (value[index], value.size()));

return num == static_cast<unsigned> (*this);

}

unsigned main()

{

const unsigned min=1;

const unsigned max=100;

std::cout << "Narcissistic numbers in the range " << min << " through " << max << ":\n\t";

for (unsigned n=min; n<=max; ++n)

if (number(n).is_narcissistic())

std::cout << n << ' ';

std::cout << '\n' << std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

C plus plus program to print number patterns?

bghjg


A program c plus plus on automorphic numbers or not?

how to write a program that counts automorphic number from 1 to 999


How to restart c plus plus program?

Exit the program and relaunch it.


What is the contact number for family health plus insurance?

(1-877-934-7587) Family Health Plus (adult insurance program)


Can you program games with c plus plus?

Yes, you can program games with C++.


Is there an answer book for the A plus program?

The A Plus Program is an initiative, not a test. So no, there is no answer book.


How do you make a program that display odd number from one to twenty in c plus plus?

for (int i=1; i&lt;20; i+=2) printf ("%d\n", i);


What is the Formula for calculating lower quartile on excel?

.25 * The Number of items in the list plus 1 equals the Item Number that is the lower quartile.


How many children does Billie Joe Armstrong have?

billie joe Armstrong has two kidz plus 999999998 so like a billion


Why does sometimes when you run a program it will execute the previous program instead the current open program in C plus plus?

Because you aren't careful enough.


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


What are the benefits of the Borders Rewards program?

There are a number of benefits to the Borders Rewards program, including coupons for in-store and online purchases, free shipping on orders above $25 and 30% off the list price of hard cover best sellers. There is also a Rewards-Plus option that increases the discount on hard cover best sellers to 40% off, 20% off the list price of selected hard cover titles, 10% off the list price of most other items and free shipping from the Borders website.