In Python, how can we determine whether an n-digit integer is an Armstrong number or not? menu

User Tag List

Results 1 to 2 of 2
  1. #1
    YreiauW's Avatar Active Member
    Reputation
    19
    Join Date
    Jul 2022
    Posts
    9
    Thanks G/R
    2/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    In Python, how can we determine whether an n-digit integer is an Armstrong number or not?

    I was attempting to answer a question. Assume a user enters an n-digit number. How do we know if it's Armstrong or not? One technique may be to record a list of all Armstrong numbers and then check from that list, but I wanted to use a different approach. This is my code:
    Code:
    #armstrong number
    take_in=int(input("Enter the number: "))
    num2=take_in
    length=len(str(take_in))
    rep=length
    summ=0
    while rep>0:
        summ=summ+(num2/10**rep)**length
        num2=num2%(10**rep)
        rep=rep-1
        if rep==0:
            if summ==take_in:
                print("{} is an armstrong number".format(take_in))
            else:
                print("{} is not an armstrong number".format(take_in))

    In Python, how can we determine whether an n-digit integer is an Armstrong number or not?
  2. #2
    JohnArmstrong's Avatar Member
    Reputation
    2
    Join Date
    Jan 2023
    Posts
    1
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is just the basics of how to calculate it in Python. Adapt it to your own purposes.

    Code:
    # Store user input and create additional variables for calculation
    User_Input = int(input("Enter an integer: "))
    User_Integer = User_Input
    Armstrong_Sum = 0
    
    
    # Find length of number. This will be used for the calculation as well as our counter.
    length  = len(str(User_Input))
    counter = len(str(User_Input))
    
    
    # Calculation
    while counter > 0:
        
        # Modulo gives the "ones" placement value for multiplication by power of length.
        Armstrong_Sum += (User_Integer % 10) ** length
        
        # Floor division to remove the existing "ones" place.
        User_Integer //= 10
        
        counter -= 1
    
    # Comparison
    if Armstrong_Sum == User_Input:
            print(True)
    else:
        print(False)
    Wikipedia also has an article on this with a Python example.
    Narcissistic number - Wikipedia

  3. Thanks YreiauW (1 members gave Thanks to JohnArmstrong for this useful post)

Similar Threads

  1. How can I determine how much my WoW Account is worth?
    By DragonfireEX402 in forum World of Warcraft General
    Replies: 8
    Last Post: 01-01-2014, 12:06 PM
  2. How can we make a safe trade here?
    By Ardamel in forum Scam Reports
    Replies: 4
    Last Post: 09-12-2013, 04:44 PM
  3. Replies: 7
    Last Post: 09-02-2012, 02:41 AM
  4. [Reskin] In MWCS, how can I find the path for Death Knights?
    By Garneth in forum WoW ME Questions and Requests
    Replies: 5
    Last Post: 08-29-2010, 04:53 PM
  5. [Req] How can we buy item with fake item ?
    By yesilmen in forum WoW EMU Exploits & Bugs
    Replies: 2
    Last Post: 08-27-2008, 07:47 PM
All times are GMT -5. The time now is 01:09 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search