Question

In: Computer Science

###### THIS SHOULD BE IN PYTHON Calculate the sum of cubes. If the input is x,...

###### THIS SHOULD BE IN PYTHON

Calculate the sum of cubes. If the input is x, the sum of cubes is equal to: 13 + 23 + ... + x3


Take input for the number of cubes to sum.
No error checking is needed.
Specifically, you may assume that the input is always a positive integer

Here are some sample runs:
Enter how many cubes to sum: 1   
1   

Enter how many cubes to sum: 3
36

Enter how many cubes to sum: 4
100

Enter how many cubes to sum: 2
9

Solutions

Expert Solution

Hope this will help you. If you have any doubt please let me know.

If you want any modification in the program please let me know.

Please go through all the notes.

Notes:

1) There are no restrictions stated in this problem statement like you should use a function.

2 ) Error checking is not done because it is stated in problem statement.

3) also not stated weather to use a for loop or while loop. (Hence I have created a 2 version of program using while loop and for loop)

4) Also you can calculate power using 2 methods, 1) using pow() function  and 2) using ** operator.

Pow() is used without math header file it is working perfectly fine in python3 and it is working in repl.lt, visual studio code and python 3.8.5 interpreter

In pow method it is calculated as pow(x,3) and, in ** operator, it is calculated as x**3 also referred as x^3.

in while loop program pow(x,3) is commented while in case of for loop ** Operator line is commented, uncomment this lines if you want to use this.

5) Every line of code is commented.

6) Screenshot of output is also attached.

7) If you want any modification in the program please let me know.

below is code for 3 version use any 1 of them

1) while (** operator)

2) while (pow() functio)

3) for loop

----------------version 1--------using while loop (** operator)----------------


x=int(input("Enter how many cubes to sum: ")) # reading an input from user
#defaul input is in string, using int() connverting it into a int
sum=0 # sum variable for storing a sum of cube upto that number
i=1 # index for while loop
while (i<=x): # using a while loop
#uncomment the next line if you want to use pow method
#y=pow(i,3) # you can also use pow method
y=i**3 # finding a cube of number using (** power operator)
sum=sum+y # adding a cube of number into previous value of sum
i=i+1 # incrementing an index value
print(sum)

-----------------------------------version 2-----using While loop (pow function)------------


x=int(input("Enter how many cubes to sum: ")) # reading an input from user
#defaul input is in string, using int() connverting it into a int
sum=0 # sum variable for storing a sum of cube upto that number
i=1 # index for while loop
while (i<=x): # using a while loop
y=pow(i,3) # you can also use pow method
#uncomment the next line if you want to use ** operator
#y=i**3 # finding a cube of number using (** power operator)
sum=sum+y # adding a cube of number into previous value of sum
i=i+1 # incrementing an index value
print(sum)


--------------------------version 3------using a for loop----------------

x=int(input("Enter how many cubes to sum: ")) # reading an input from user

#defaul input is in string, using int() connverting it into a int

sum=0 # sum variable for storing a sum of cube upto that number

#for loop ranges from 0 to x-1  when we have written range(x) in it, to include x in cube we have to write x+1

# this will iterate till (0 to x)

for i in range(x+1): # using a for loop,

       y=pow(i,3) # you can also use pow method

       #uncoment the next line if you want to use ** operator

       #y=i**3 # finding a cube of number using (** power operator)

       sum=sum+y # adding a cube of number into previous value of sum

print(sum)

---------------------Screenshot-------------



Related Solutions

Python programming: Instructions: The python program should respond to user input on a command line Below...
Python programming: Instructions: The python program should respond to user input on a command line Below are the four options - if the user input is A, the program will quit -if the user input is B, the program will display the number of times the prompt has been displayed -if the user input is C, will display whatever is stored. -if the user inputs something else, it will store that user input as a string and append it to...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
complete in python The function sumEven should return the sum of only the even numbers contained...
complete in python The function sumEven should return the sum of only the even numbers contained in the list, lst. Example list_of_nums = [1, 5, 4, 8, 5, 3, 2] x = sum_evens(list_of_nums) print(x) #prints 14 Start your code with def evens(lst):
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5!...
1) Write an algorithm to calculate the sum of the following series: Sum =x-x3/3! + x5/5! – x7/7! +……. Stop when the term<0.0001. 2) An internet service provider charges its subscribers per month as follows: Data usage (n), in gbs charges (NIS) 0.0<n<=1.0 250 1.0<n<=2.0 500 2.0<n<=5.0 1000 5.0<n<=10.0 1500 n>10 2000 Write a C program to read the usage(n) from a file and print the charges to be paid by the subscribers. Your program must include the function calculate...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Use python write a function that translates the input string into Pig Latin. The translation should...
Use python write a function that translates the input string into Pig Latin. The translation should be done word by word, where all words will be separated by only one space. You may assume that each word must have at least one vowel (a,e,i,o,u and uppercased counterparts), and there will be no punctuation or other special characters in the input string. The Pig Latin rules are as follows: For words that begin with consonants, all letters before the initial vowel...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number...
(Python) Implement a function to compute a sum that can compute sum for an arbitrary number of input integers or float numbers. In other words, calls like this should all work: flexible_sum(x1, x2) # sum of two integer or float numbers or strings x1 and x2 flexible_sum(x1, x2, x3) # sum of 3 input parameters and can also handle a single input parameter, returning it unchanged and with the same type, i.e.:   flexible_sum(1) returns 1 and can accept an arbitrary...
MATLAB QUESTION Write a python program to request a positive float input from the user, x....
MATLAB QUESTION Write a python program to request a positive float input from the user, x. The program should check for the value of x. If x is larger than or equal to 1.0, the program should display the value of x in addition to the string “is larger than or equal to 1” and then terminate the program. If x is less than 1 the program should calculate y such that: y = 1-x+x^2/2-x^3/3+x^4/4-x^5/5……-x^99/99+x^100/100 The program should then display...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember,...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember, n is a scalar quantity (float/int). x is a NumPy array and you should return an equal length NumPy array :param x: numpy array :param max_val: The maximum value :return: a tuple with two numpy arrays x and the sum """
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT