Question

Python code

Write a Python program that takes the value of n from the user and 
calculates the value of S using the following equation:
S = 1! - 2! + 3! - 4! + 5! - 6! + ……..  n!
Here, the value of n should be a positive (n>0) integer. If the user gives 
a negative input or zero, then print “Invalid Input!”. Also, print the 
sentence “End of program.” in the last line no matter what the input is.
---------------------------------------------------------------------
Sample Input 1:
4
Sample Output 1:
Value of S is: -19
End of program.
---------------------------------------------------------------------
Sample Input 2:
11
Sample Output 2:
The value of S is: 36614981
End of program.
---------------------------------------------------------------------
Sample Input 3:
-4
Sample Output 3:
Invalid Input!
End of program.
---------------------------------------------------------------------
Explanation/Hints:
You may import the ‘math’ module and use the built-in math.factorial() 
function in this problem. Alternatively, if you use a nested loop, the 
equation of S can be rewritten as follows:
S = 1 - (1*2) + (1*2*3) - (1*2*3*4) + …….. (1*2*3….*n)

For Sample Input 1 & 2, if you simply implement the given equation in your 
code, you will get the desired values. For Sample Input 3, as the input is 
negative, the corresponding output is displayed. The sentence “End of 
program.” is displayed every time.

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

def factorial_val(num):   
if num > 1:   
return factorial_val(num-1) * num
else:   
return num
  

limit = int(input("enter number of terms : "))
if(limit>0):
result = []   
count = 1
while(count <= limit):
result.append(factorial_val(count))   
count += 1
  
s=0
for i in range(0,limit):
if i%2==0:
s+=result[i]
else:
s-=result[i]
print(result)
print("sum of series : " , s)

else:
print("Enter a positive number invalid input")


answered by: Ronodeep Bhowmik
Add a comment
Know the answer?
Add Answer to:
Python code
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """...

    PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • *This question was posted earlier but I want to make sure my code differs from the...

    *This question was posted earlier but I want to make sure my code differs from the other answer* CS50 Assignment 3 and Assignment 4 (Both Due By Sunday March 29 11:59 PM) Assignment 3: Bank Application In a recent lecture, we discussed getting input from the user by using the scanf() function. For your reference, I have included a simple example of using scanf() below: scanf() example: #include int main(void) { int number; printf("Enter a number: "); //prompt the user...

  • Only use the code in Objectives and write the code in Python

    Only use the code in Objectives and write the code in Python Question 1. Study the examples below carefully and write a program for journal subscription Your program should work exactly as the following examples The text in bold indicates user input. Example 1: The user is a student, and the user subscribes to 2 journals Question 1 - Journal of Commodity Markets Journal of Sustainable Mining Journal of Asia-Pacific Biodiversity Student $21 $22 $27 Non-student $50 $60 $75 Are...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • MIPS CODE required to write an assembly program to find the maximum of an array of integers by...

    required to write an assembly program to find the maximum of anarray of integers by doing the following:1. Prompt user to input array size n (n <= 10)2. Prompt user to input element values of array A one by one3. Display the result on the console.This program must at least include one function. The main program will read the valuesof the array (as user inputs the element values) and stores them in the memory (datasegments section) and at the end...

  • Write the code in Python. The output should be exact the same as the given.

    Write the code in Python. The output should be exact the same as the given. Question 1. Study the examples below carefully and write a program for journal subscription Your program should work exactly as the following examples The text in bold indicates user input. Example 1: The user is a student, and the user subscribes to 2 journals Question 1 - Journal of Commodity Markets Journal of Sustainable Mining Journal of Asia-Pacific Biodiversity Student $21 $22 $27 Non-student $50...

  • write programs with detailed instructions on how to execute. code is java What you need to...

    write programs with detailed instructions on how to execute. code is java What you need to turn in: You will need to include an electronic copy of your report (standalone) and source code (zipped) of your programs. All programming files (source code) must be put in a zipped folder named "labl-name," where "name" is your last name. Submit the zipped folder on the assignment page in Canvas; submit the report separately (not inside the zipped folder) as a Microsoft Word...

  • Write a program in python that asks the user to input a sentence. The program will...

    Write a program in python that asks the user to input a sentence. The program will ask the user what two letters are to be counted. You must use a “for” loop to go through the sentence & count how many times the chosen letter appears in the sentence. You are not allowed to use python built-in function "count()" or you'll get a Zero! Output will show the sentence, the letter, and the number of times the letter appears in...

  • I need to write a paint job estimator program in python. I have most of it...

    I need to write a paint job estimator program in python. I have most of it down but i keep getting errors and I dont know how to fix it or what im doing worng specs: A painting company has determined that for every 350 square feet of wall space, one gallon of paint and six hours of labor are required. The company charges $62.25 per hour for labor. Write a program call paintjobestimator.py that asks the user to enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT