5.Write pseudocode for the algorithm using iteration (looping) and make a flow chart and python code from your pseudocode.
Envision an algorithm that when given any positive integer n, it will print out the sum of the squares from 1 to n. For example given 3 the algorithm will print 14 (because 1 + 4 + 9 =14) You can use multiplication denoted as * in your solution and you do not have to define it (For example. 3*3=9) For example if n is 6 it will print: The sum of squares from 1 to 6 = 91 Again, to get marks, the algorithm needs to work for any n given.
PSEUDOCODE ::
Input the positive integer n
k=n , sum = 0
while n>0
sum = sum +(n*n)
n=n-1
Print : The sum of squares from 1 to k = sum
PYTHON CODE :
# To take the desired integer as input
n=int(input())
# initialising the value to 0
sum = 0
# stored the value of n in another variable
k=n
# checking whether n is > 0 or not
while(n > 0) :
sum=sum+(n*n) # adding square of the present value of n to
sum
n=n-1 # decrement the value of n
# printing the output
print('The sum of squares from 1 to {0} = {1}'.format(k, sum))
FLOWCHART ::
Get Answers For Free
Most questions answered within 1 hours.