Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Give the Python program that computes the square of a number. The number is p

ID: 3673916 • Letter: 1

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 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 .

Explanation / Answer

1) Python code to find out the square of number is given below:

def square(value):
    return value*value

n = square(7)
print(n)

2)Python code for finding out the sum of square of n numbers

def sumOfSquare(value):
    sum = 0
    i = 1
    j = int(value)
  
    while (i<=j):
        sum = sum + i*i
        i = i+1
  
    return sum

n = input('Enter the value of n: ')
squareSum = sumOfSquare(n)
print(squareSum)