Hello Friends,
I have got a program for Checking the number that is it Armstrong or not. and to genrate a list of armstrong no. below 1000. The program is in C++.

Dani AI

Generated

: the usual task is twofold — test whether a given nonnegative integer is an Armstrong (narcissistic) number, and generate all such numbers below 1000. was right to ask for the precise goal. guessed there was an online reference; the simplest, robust rule is: an n‑digit number equals the sum of its digits each raised to the n power. Single‑digit numbers (0–9) meet that rule trivially.

Here is a compact, safe C++ solution (prints Armstrong numbers < 1000):

#include <iostream>

bool isArmstrong(int n) {
    if (n < 0) return false;
    int t = n, digits = 0;
    if (t == 0) digits = 1;
    while (t) { ++digits; t /= 10; }
    t = n;
    int sum = 0;
    while (t) {
        int d = t % 10;
        int p = 1;
        for (int i = 0; i < digits; ++i) p *= d;
        sum += p;
        t /= 10;
    }
    return sum == n;
}

int main() {
    for (int i = 0; i < 1000; ++i)
        if (isArmstrong(i)) std::cout << i << '\n';
    return 0;
}

Notes and troubleshooting:

  • This avoids floating pow() to prevent rounding errors; integer multiplication is exact for these ranges.
  • To skip trivial single‑digit results, start the loop at 10 or 100 depending on whether you want 2/3‑digit Armstrongs only.
  • For much larger ranges, precompute digit^k for k up to the max digit count to speed checks and avoid repeated work. Watch integer overflow when exploring very large numbers.
  • Expected Armstrong numbers below 1000 (including single digits): 0,1,2,3,4,5,6,7,8,9,153,370,371,407.

Recommended Answers

All 2 Replies

er, so, what was the question?

Hello Friends,
I have got a program for Checking the number that is it Armstrong or not. and to genrate a list of armstrong no. below 1000. The program is in C++.

Great work! So you copied it from here then ported it to c++?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.