Question

hw1) Write a python script using for loop, which calculates the sum of the squares of...

hw1) Write a python script using for loop, which calculates the sum of the squares of the numbers from 5 to 23. (i.e. 52+62+72+ ... +222+232)

hw2) try to calculate the same in hw1 using the while loop instead of for loop.

Homework Answers

Answer #1

hw1)

def squaresum() : 
  
    # Iterate i from 5  
    # to 23 finding  
    # square of i and 
    # add to sum. 
    sm = 0
    initial = 5
    final = 23
    for i in range(initial, final+1) : 
        sm = sm + (i * i) 
      
    return sm 
  
# Driven Program 
print(squaresum())

hw2)

def squaresum() : 
  
    # Iterate i from 5  
    # to 23 finding  
    # square of i and 
    # add to sum. 
    sm = 0
    initial = 5
    final = 23
    while(initial != final+1): 
        sm = sm + (initial * initial)
        initial = initial + 1
      
    return sm 
  
# Driven Program 
print(squaresum())

Drop a comment for any clarification or update, I would love to assist you.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Python code Write a complete program with for loop which calculates the sum of the numbers...
Python code Write a complete program with for loop which calculates the sum of the numbers from 1 to 100
Write a Python program using while loop that finds and prints the sum of this geometric...
Write a Python program using while loop that finds and prints the sum of this geometric series. 1 + 2 +4 +8 + … + 1024
Python Question: Write a function which returns the sum of squares of the integers 1 to...
Python Question: Write a function which returns the sum of squares of the integers 1 to n. For example, the sum of the squares from 1 to 4 is 1 + 4 + 9 + 16, or 30. You may choose what should be returned if n == 0 You may not use the built-in Python sum function. The Solution Must be recursive >>> sum_of_squares(1) 1 >>> sum_of_squares(4) 30 >>> sum_of_squares(8) # 64 + 49 + 36 + ... +...
Write a Python program which calculates the average of the numbers entered by the user through...
Write a Python program which calculates the average of the numbers entered by the user through the keyboard. Use an interactive loop and ask the user at each iteration if he/she wants to enter more numbers. At the end dispay the average on the screen. Using built-in library functions for finding average is not allowed. Sample output of the program: Enter a number > 23 More numbers (yes or no)? y Enter a number > 4 Do you have more...
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum...
Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers, if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at least...
Write a PowerShell script to get a Summary of 1 to 1000. i.e. Calculate the total...
Write a PowerShell script to get a Summary of 1 to 1000. i.e. Calculate the total of “1+2+3+4+5+6+7…+998+999+1000” You must use loop to do the calculation. Hint: The total is “500500” Manual summary of $Sum=1+2+3+4… is NOT acceptable. Using formula like $Sum= n(n+1)/2 is NOT acceptable.
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
This program is for kindergartners to practice knowledge on numbers divisible by 5. Write a code...
This program is for kindergartners to practice knowledge on numbers divisible by 5. Write a code using a while loop, which keeps asking the user to enter an number divisible by 5. The program(i.e the while loop) terminates when the user enters a number divisible by 5, but keeps running if the user fails to enter a number divisible by 5. Also print appropriate message after each failed try. When successful, tell the user how many attempts were needed for...
1) Write a java programming nested while loop where you will declare two numbers one for...
1) Write a java programming nested while loop where you will declare two numbers one for outer loop and the other one for inner while loop, and display the result. (Try using scanner) Steps: 1) Declare the variable of outer loop int and assign value of your choice 2) Declare while condition which outer variable is less the number of your choice 3) Declare variable of inner loop int and assign the value of your choice 4) Declare while condition...
Finally, write a script named word_counts.sh that prints out how many words are on each line...
Finally, write a script named word_counts.sh that prints out how many words are on each line of standard input, as well as the total number of words at the end. The script should take no arguments. Instead, the script must work by reading every line of standard input (using a while loop) and counting the number of words on each line separately. The script is intended to work with standard input coming from a pipe, which will most often come...