Question: 1. Give the Python program that computes the square of a number. The number is passed to the function as a parameter. The output is

1. Give the Python program that computes the square of a number. The number is passed to the function as a parameter. The output is the square of this number. For example, the function can be used as shown below

. . . .

n = s q u a r e ( 7 )

. . .

2. Another very useful feature of Python is looping, which is more flexible than that available in BB.

For example:

while i <10:

p r i n t ( i )

will give the following output:

0

1

2

3

4

5

6

7

8

9

In this experiment, give the Python program that outputs the sum of squares of numbers from 1 to n, i.e. y = 12 + 22 + 32 + ... + n2

where n is specified by the user at run time.

Hints:

y can be computed using a loop, say, while.

Before you can use the while loop, you need to initialize y with 0.

In the ith iteration of the loop, add i2 to y, i.e. y = y + i2 .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!