Python

 Use the IDLE editor for a Python program with three functions:

  • A function named square that accepts a number as an argument and returns the square of that number. (Remember, the square of a number is the number multiplied by itself. For example, 2 squared is 2 x 2 = 4.)
  • A function named cube that accepts a number as an argument and returns the cube of that number. (Remember, the cube of a number is the number multiplied by itself twice. For example, 2 cubed is 2 x 2 x 2 = 8.)
  • A main function that uses a loop to display the numbers from 1 to 10, and their squares, and their cubes. The output should be formatted as a table as shown below.
  • The main function must call the square function and the cube function to calculate the square and cube of each number.
  • The output must be printed from the main function with column headings as shown below.
  • The numbers in the columns should be right-aligned.

Answer-

#function named square that accepts a number as an argument and returns the square of that number
def square(x):
    return x*x
#function named cube that accepts a number as an argument and returns the cube of that number
def cube(y):
    return y*y*y
print("Number  Square  Cube")
i = 0
#loop to display the numbers from 1 to 10, and their squares, and their cubes
while i <= 10:
    print("{:<8d}{:<8d}{:d}".format(i, square(i), cube(i)))
    i += 1
    

 

Output-

Number  Square  Cube
0       0       0
1       1       1
2       4       8
3       9       27
4       16      64
5       25      125
6       36      216
7       49      343
8       64      512
9       81      729
10      100     1000

Comments

Popular posts from this blog

The python program allows the user to enter the Sales amount and Actual cost of a Product. Next, Python calculates the Loss Amount or profit Amount based on those two values using if-elif-else Statement

Mealy and Moore Machine

Question Bank of Theory of Automata & Formal Languages