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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started