answersLogoWhite

0

C plus plus program to list all Armstrong number?

Updated: 8/11/2023
User Avatar

Habta

Lvl 1
βˆ™ 10y ago

Best Answer

#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

βˆ™ 9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 10y ago

#include <iostream>

#include <math.h> // for std::pow()

unsigned int get_length(unsigned int num,const unsigned int base=10)

{

unsigned int len=1;

while(num && (num/=base))

++len;

return( len );

}

bool is_armstrong(const unsigned int num,const unsigned int base=10)

{

unsigned int len=get_length(num,base);

unsigned int sum=0;

unsigned int tmp=num;

while(tmp)

{

sum+=(unsigned int)std::pow((double)(tmp%base),(double)len);

tmp/=base;

}

return(num==sum);

}

int main()

{

std::cout << "Armstrong series (base 10):";

for(unsigned int num=0; num<=0xffffffff; ++num)

if(is_armstrong(num))

std::cout << " " << num;

std::cout << std::endl;

return(0);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C plus plus program to list all Armstrong number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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)


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.


Can you program games with c plus plus?

Yes, you can program games with C++.


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);


How many children does Billie Joe Armstrong have?

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


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.


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.