Q:

Write a python program to rint the cube of all numbers from 1 to a given number

belongs to collection: Python Loop Exercises

0

Calculate the cube of all numbers from 1 to a given number

Write a program to rint the cube of all numbers from 1 to a given number

Given:

input_number = 6

Expected output:

Current Number is : 1  and the cube is 1
Current Number is : 2  and the cube is 8
Current Number is : 3  and the cube is 27
Current Number is : 4  and the cube is 64
Current Number is : 5  and the cube is 125
Current Number is : 6  and the cube is 216

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Iterate numbers from 1 to n using for loop and range() function
  • In each iteration of a loop, calculate the cube of a current number (i) by multiplying itself three times (c = i * i* i)

Solution:

input_number = 6
for i in range(1, input_number + 1):
    print("Current Number is :", i, " and the cube is", (i * i * i))

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program to calculate the sum of series up ... >>
<< Use a loop to display elements from a given list p...