Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python Armstrong Number print

Status
Not open for further replies.

soni21

Programmer
Apr 25, 2023
9
IN
I need to print all armstrong numbers ranging from 1 to 10000. My issue is that when my programme runs and hits 150, it does nothing.

Python:
(1^3) + ((6^3)-2) + (0^3)

as opposed to

Python:
(1^3) + (6^3) + (0^3)

As a result, it does not display 153 (an Armstrong number), because the total is 152. I'm not sure whether other numbers are performing the same thing. Yet, as shown here, I checked up to 200 and there is no problem with any other numbers except those in the 150-160 area.

Is this a compiler bug? Is it necessary to reinstall my compiler? I'm currently utilising codeblocks.

Python:
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    for(int i = 0;i <= 1000;++i)
    {
        int r = i;
        int dig = 0;
        while(r != 0)
        {
            dig++;
            r /= 10;
        }
        int n = i, sum = 0;
        while(n != 0)
        {
            int d = n % 10;
            sum += pow(d, dig);
            n /= 10;
        }
        if(sum == i)
            cout << i << ' ';
    }
    cout << "\n\n\n";
    return 0;
}


 
Hi soni21,

The power operator in python is not ^ but **:
>>> 1**3 + 5**3 + 3**3
153

The code on the link you provided works well

I tried it
Code:
# [URL unfurl="true"]https://www.scaler.com/topics/armstrong-number-in-python/[/URL]

def check_3_digit_armstrong(number):
  temp = number
  add_sum = 0
  while temp != 0:
    k = temp % 10
    add_sum += k*k*k
    temp = temp//10
  if add_sum == number:
    print('The number %3d is a three-digit Armstrong Number' % number)

def check_n_digit_armstrong(number):
  digits = len(str(number))
  temp = number
  add_sum = 0
  while temp != 0:
    # get the last digit in the number
    k = temp % 10
    # find k^digits
    add_sum += k**digits
    # floor division
    # which update number with second last digit as last digit
    temp = temp//10
  if add_sum == number:
    print('The number %6d is %d-digit Armstrong Number' % (number, digits))
    
if __name__ == "__main__":
  print("3-digit Armstrong numbers")
  for number in range(100, 1000):
    check_3_digit_armstrong(number)

  print()

  print("n-digit Armstrong numbers")
  for number in range(100, 1000001):
    check_n_digit_armstrong(number)

and got this output:
Code:
3-digit Armstrong numbers
The number 153 is a three-digit Armstrong Number
The number 370 is a three-digit Armstrong Number
The number 371 is a three-digit Armstrong Number
The number 407 is a three-digit Armstrong Number

n-digit Armstrong numbers
The number    153 is 3-digit Armstrong Number
The number    370 is 3-digit Armstrong Number
The number    371 is 3-digit Armstrong Number
The number    407 is 3-digit Armstrong Number
The number   1634 is 4-digit Armstrong Number
The number   8208 is 4-digit Armstrong Number
The number   9474 is 4-digit Armstrong Number
The number  54748 is 5-digit Armstrong Number
The number  92727 is 5-digit Armstrong Number
The number  93084 is 5-digit Armstrong Number
The number 548834 is 6-digit Armstrong Number
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top