Computer Science, asked by armyaixhuu, 1 year ago

using python input a 3 digit number and print its sum of each digits of its cube Eg: 325=3^3+2^3+5^3=160

Answers

Answered by fiercespartan
8

First we take the input from the user and I will provide a step by step process to get this right.

(1) Break every number in the number, to do that, I will use list() command. This separates every character but before using this, we will need to convert the number into a string.

(2) Convert all the strings after using the list() into integers

(3) Find the cubes of the numbers and add them up.

1st step:

number = input()

We are taking the number as a string but if we don't specify we just want number, there could be alphabets and other characters entered too.

number = list(number)

2nd step:

number = [int(x) for x in number]

We converted all the digits back to numbers

3rd step:

We know that we will only have 3 digits, and knowing this would make it easier.

cube = number[0]**3 + number[1]**3 + number[2]**3

print(cube)

_______________

We are done, but if we want more of a restrictive code, I mean while taking input, we only allow numbers and not more than 3 digits, here is the code for that:

CODE:

while True:

number = int(input())

if len(str(number)) == 3:

 break

number = str(number)

number = list(number)

number = [int(x) for x in number]

cube = number[0]**3 + number[1]**3 + number[2]**3

print(cube)

______________

What this does is it keeps taking the input until it matches all the criteria. They are:

(1) Has to be a number, no decimal

(2) has to be a 3 digit number

Similar questions