In: Computer Science
ALL IN PYTHON PLEASE
Problem 1: Accept one integer from the user and store that information in a variable called endNo. Use an IF structure to check that endNois a positive number. If endNo is negative, print an error message and stop. If it is positive, use a loop (for or while) to generate odd numbers from 1 to endNo. Calculate the sum of these numbers using an accumulator.
Rubric:
Data input and data type conversions: 2.5 pts
Use of IF structure to verify that endNo is positive: 2.5 pts
Use of loop to generate numbers from a (inclusive) to b (not
inclusive): 5 pts
Use of an accumulator (note: print the cumulative sum only once): 5
pts
Problem 2:
Modify problem 1 and allow the user to specify the (a) starting value for the loop, (b) stopping value for the loop, and (c) the step function. Please note that the stopping value must be included in the calculation of your running total. Example of the desired solution:
Enter starting value: 5
Enter stopping value: 15
Enter step value: 5
30
In the above example, the result (30) is the sum of 5 + 10 + 15.
Note that both the starting value and ending value are included in
the running total. The step value determines the series.
Rubric:
Modification of the loop function: 5 pts
Problem 3:
Define a VALUE-RETURNING function that accepts one parameter - an integer number. The function, which must be a value-returning function, returns 1 if the number is even or 0 if the number is odd. In the “main” function (i.e. def main()), capture the return value and print an appropriate message on screen (i.e. number is even or odd).
Rubric:
Correctly defined a value-returning function: 5 pts
Correctly capture and use return value from a value-returning function: 5 pts
Sample Output:
Enter n: 4
Even
Problem 4
Write a program to compute the area of a circle some 'n' times. You must accept n and r from the user. Area is calculated using (22/7.0)*r*r - where r is the radius. Implement using value-returning functions. Hint: create a function to calculate area taking r as a parameter. In the main() function, ask for n and create a loop where you input r and invoke the area function n times.
Rubric:
Correct use of a loop to perform n calculations for the user: 5
pts
Correct use of value-returning functions inside a loop structure:
10 pts
Sample Output:
Enter n: 3
Enter r: 34
Area is: 3633.14285714
Enter r: 23
Area is: 1662.57142857
Enter r: 43
Area is: 5811.1428571
#Please ask 1 question at a time, i have answered 1st 2
#1
endNo = int(input('Enter an integer: '))
if endNo<=0:
print('Error: integer entered must be positive!!!')
else:
total=0
n=1
while n<=endNo:
total+=n
n+=2
print(total)
#2
start=int(input('Enter starting value: '))
end=int(input('Enter stopping value: '))
step=int(input('Enter step value: '))
total=0
num=start
while num<=end:
total+=num
num+=step
print(total)