Question

In: Computer Science

1. (30 pts) Use a sum accumulator, a for loop and a range statement to find...

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!]

Solutions

Expert Solution

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


Related Solutions

Write a complete program to sum the following series: (hint: use the for loop) 1 /2...
Write a complete program to sum the following series: (hint: use the for loop) 1 /2 + 1/ 4 + 1 /6 + ⋯ + 1 /100 PS: use C++ language please
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
(22 pts) Find the experimental probability of rolling each sum. Fill out the following table: Sum...
(22 pts) Find the experimental probability of rolling each sum. Fill out the following table: Sum of the dice Number of times each sum occurred Probability of occurrence for each sum out of your 108 total rolls (record your probabilities to threedecimal places) 1 2 3 4 5 6 7 8 9 10 11
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Python Write a for loop with a range function and format output as currency Use an...
Python Write a for loop with a range function and format output as currency Use an input statement to ask the user for # of iterations using the prompt: #? [space after the ?] & assign to a variable Convert the variable to an integer Use a for loop and a range function to display all whole numbers from 1 to the user entered number Display the value of the item variable on screen in each iteration in the following...
Use a for loop to determine the sum of the rst 10 terms in the series 5k3, k % 1, 2, 3, . . . , 10.
Use a for loop to determine the sum of the rst 10 terms in the series 5k3, k % 1, 2, 3, . . . , 10.
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Given the sample data. x: 21 19 15 30 27 (a) Find the range. Range =...
Given the sample data. x: 21 19 15 30 27 (a) Find the range. Range = 15 (b) Verify that Σx = 112 and Σx2 = 2,656. Σx = Σx2 = (c) Use the results of part (b) and appropriate computation formulas to compute the sample variance s2 and sample standard deviation s. (Round your answers to two decimal places.) s2 = s = (d) Use the defining formulas to compute the sample variance s2 and sample standard deviation s....
Use python Only can use if statement and loop, that's professor's requirement I have no idea...
Use python Only can use if statement and loop, that's professor's requirement I have no idea how to do that without class Building Entrance Tracker Due to Covid-19, we can allow only 5 guests to our building at a time. Our task is to write a program that helps the entrance staff to keep track of guest entries to the building. Feature 1 (40 points): Each guest must sign in upon entry into the building. If the number of guests...
Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where...
Find a loop invariant (I) for the following while loop with the post-condition {Q: S=1}, where S is an integer and the operator / represents an integer division. Show your work step by step to receive full credit. while (S > 1) do S = S / 2; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT