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.
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")
PYTHON Write a program to enter a valid variable till the user wants to end. The program should display the count of positive, negative, zeros and letters (upper and lower case)/symbols (error handling) entered. Based on the problem, you need to design an algorithm as follows: 1. Get the user input until “n” is entered 2. Add to the positive if it is greater than zero 3. Add to the negative if it is less than zero 4. Add to...
PLEASE DO BOTH OF THESE IN PYTHON!!! AND MAKE SURE THAT I CAN COPY THE CODE. 1.) Exercise #1: Design and implement a program (name it PrintSum) that prompts the user for an integer values between 1 and 100. The program then uses a while loop to determine and print out the sum of all values between 1 and the entered value. The program rejects invalid input values with proper message such “Invalid Input. Try again.” Document your code and...
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 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 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
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...
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 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”...
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 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...