In: Computer Science
1. (30 pts) Use a sum accumulator, a for loop and a range statement to find the sum of the odd ints from 111 to 815, inclusive. Use a print statement to print out a labelled answer: "The sum of odd ints from 111 to 815, inclusive, is _______."Do not use formulas to add up the ints, just add them up please. Copy the answer from the shell into your file, comment the answer out. Show how you can check your code. [Hint: one approach is to take a similar but smaller problem. Another approach is to use the formula for the sum of an arithmetic sequence – this is OK to check, but not for the original answer.]
2. (30 pts) Write python code to solicit four numbers from the user and to print out the sum of the squares of the 4 numbers shown below. For full credit, use a sum accumulator and a for loop. Have the user type in the 4 numbers 1950.1, 1970.2, 1990.3, 2010.4 when prompted and then print out the sum of the squares of these number using a labelled print. Copy the answer from the shell into your file, comment the answer out. Show how you can check your code. [Hint: initialize a sum accumulator, set up a for loop. Inside the loop, solicit an input from the user, then update the sum accumulator.]
3.Find the following sum using a sum accumulator and a for loop:1/1 – ½ + 1/3 – ¼ + 1/5 – 1/6 .... + 1/99 - 1/100
Print a labelled output, put the output in your code and comment it out. Use the round function to round your sum to 4 decimal points.Show how might you check your answer.[Hints: initialize a sum accumulator. Set up a for loop with a range statement. How many times should you go through the loop? What should the range be? Inside the loop, compute the current fraction and add it to the sum accumulator. You will need a mechanism to alternate the signs of the fractions being added. One way to make the sign alternate is to use (-1)**n, where n is an int – the sign of this expression alternates as n changes from even to odd – check it out!]
I have implemented Question 1,2,3 as per the requirements.
PLEASE FIND THE FOLLOWING CODE SCREENSHOT, OUTPUT, AND CODE OF EACH
QUESTION.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
QUESTION 1:
1.CODE SCREENSHOT:
2.OUTPUT:
3.CODE:
#Staring number
a=111
#ending number
b=815
#Initilize sum to 0
sum=0
#for range 'a' to 'b+1' because range will use b exclusive
for n in range(a,b+1):
#If the number is odd then sum the number
if n%2!=0:
sum=sum+n;
#Print the result in the required form
print("The sum of odd ints from "+str(a)+" to "+str(b)+" , inclusive, is : "+str(sum))
QUESTION 2:
1.CODE SCREENSHOT
2.OUTPUT:
3.CODE
#Initilize temporary variable to zero
t=0
#Initilize the sum
sum=0;
#Loop to read 4 numbers and sum the square of the given numbers
for i in range(0,4) :
t=float(input('Enter the Number : '))
sum=sum+t*t;
#print the labled output
print("The Sum of the Squares is : "+str(sum))
QUESTION 3:
1.CODE SCREENSHOT:
2.OUTPUT:
3.CODE
#Initilize the sum to 0
sum=0
#Loop from 1 to 100
for i in range(1,101):
#Add each term to sum
sum+=(1/i)*(-1)**(i+1)
#Print the output
print("The Sum of 1/1 – ½ + 1/3 – ¼ + 1/5 – 1/6 .... + 1/99 - 1/100 = "+str(sum))
ANY CLARIFICATIONS/MODIFICATIONS REQUIRED LEAVE A COMMENT
IF YOU ARE SATISFIED WITH THE ANSWER PLEASE LIKE THE POST