Use Python Language to: Print the sum of all the prime numbers between 30 and 50
ID: 3682973 • Letter: U
Question
Use Python Language to:
Print the sum of all the prime numbers between 30 and 50.
Print the following sequence:
1
1 1 1
1 1 1 1
1 1 1 1 1
1 1 1 1
1 1 1
1 1
1
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms are:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Design and write the program that calculates this sequence. Then modify the program to find the sum of the odd-valued terms.
Input two numbers from the keyboard and find the following by creating a function for each mathematical operation.
i) addition( )
ii) subtraction( )
iii) division()
iv) multiplication
The division( ) function should report an error upon dividing by zero(0).
Explanation / Answer
Prime Number program
for num in range(30, 50):
for i in range(2,num):
if (num%i==0):
break
else:
print(num)
Fibonacci series
def recur_fibo(n):
"""Recursive function to
print Fibonacci sequence"""
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
# take input from the user
nterms = int(input("How many terms? "))
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
Python Arithmactic Operators example
# This program adds two numbers provided by the user
# Store input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')
# Add two numbers
sum = float(num1) + float(num2)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
# Program make a simple calculator that can add, subtract, multiply and divide using functions
# define functions
def add(x, y):
"""This function adds two numbers"""
return x + y
def subtract(x, y):
"""This function subtracts two numbers"""
return x - y
def multiply(x, y):
"""This function multiplies two numbers"""
return x * y
def divide(x, y):
"""This function divides two numbers"""
return x / y
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = input("Enter choice(1/2/3/4):")
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
elif choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
elif choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
elif choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.