Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using python 3 ---- Iteration Ask a user to enter a number. Print the sum of the

ID: 3863009 • Letter: U

Question

using python 3

----

Iteration Ask a user to enter a number. Print the sum of the SQUARES of the integers from 0 up to and including their number. Ex: if the user enters 4, output is 30 (0 + 1^2 + 2^2 + 3^2 + 4^2 = 30). Keep asking the user to enter a number until they enter-999 and the program should print the number and that number cubed. After the loop is exited, print the sum of the cubes of all of the numbers. Example run: Enter a number -999 to stop: 2 2 8 Enter a number -999 to stop: 4 4 64 Enter a number -999 to stop: -999 Sum of cubes: 72 What is the EXACT output? ct = 10 while ct

Explanation / Answer

Iteration:

Solution 1: Following is the function:

def sumOfSquares(): # name of function is sumOfSquares
print("Enter the input: ") # asking user to enter the input
number = int(input()) #scanning the input from user into variable number
sumSquares = 0 # declaring and defining a variable sumSquares that will store the sum of squares
for i in range(0,number+1): #loop to calculate the squares from 0 to number
sumSquares = sumSquares + (i*i) # sumSquares is updated with sum of sumSquares and square of loop variable i
print("Sum of Squares from 0 to",number,"is",sumSquares) # finally printing the output sumSquares.

Solution 2:

the function for the given task is as follows:

def sumOfCubes(): # name of function is sumOfCubes
print("Enter a number -999 to stop: ") # asking user to enter the input, -999 to stop
number = int(input()) #scanning the input from user into variable number
sumCubes = 0 # declaring and defining a variable sumCubes that will store the sum of cubes
while(number != -999): #loop to take the input from user until he enters -999 as input to stop
cubeOfNumber = number * number * number #cubeOfNumber is assigned, cube of number which is input from the user
sumCubes = sumCubes + cubeOfNumber # sumCubes is updated with sum of sumCubes and cube of input variable number
print(number,cubeOfNumber) #printing number and its cube to the screen
print("Enter a number -999 to stop: ") # asking user to enter the input, -999 to stop
number = int(input()) #scanning the input from user into variable number
print("Sum of cubes:",sumCubes) # finally printing the output sumCubes to the screen

Solution 3:

The exact output for the given piece of code is: 12 15 18

See the comments to see what each line means.

ct = 10 #initializing a varible ct to 10
while(ct < 20): #loop that runs if value of ct is less than 20
if ct%3 == 0: #if condition to check if ct is a multiple of 3, i.e. if remainder of ct/3 is equals to 0
print(ct, end = " ") #if the above if condition is satisfied then this statement will execute and it will print ct to the screen. Please note that end = " " is used so as to prevent the cursor from going to the next line, and continue printing in the same line.
ct += 1 #updating the value of ct and control goes back to the while statement