In: Computer Science
###### 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
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-------------