Write the following functions: (5 points for each function)
a function that takes an integer n and prints all numbers (1 to n inclusive) separated by a space.
a function that takes an integer n and prints all numbers (n to 1 inclusive) in backward separated by a space.
a function that takes an integer n and returns the sum of first n odd numbers (1 to n inclusive).
a function that takes an integer n and returns the sum of first n even numbers (1 to n inclusive).
Write a program that reads a number form the keyboard. Call each of the above functions. Sample output with n = 10
Enter a number 10
print Numbers Forward: 1 2 3 4 5 6 7 8 9 10
print Numbers Backward: 10 9 8 7 6 5 4 3 2 1
sum of odd numbers: 25 sum of even numbers: 30
Write all the above 4 functions in a .py file.
n = int(input("Enter a number "))
print("Numbers Forward:")
def forward(n):
for x in range (1, n+1):
print(x, end = " ")
return n
forward(n)
print()
print("Numbers Backward:")
def backward(n):
for x in range (n, 1-1, -1):
print(x, end = " ")
return n
backward(n)
print()
print("Sum of Odd Numbers:")
sum = 0
for x in range (1, n+1, 2):
sum = sum + x
print(sum)
print("Sum of Even Numbers:")
sum = 0
for x in range (0, n+1, 2):
sum = sum + x
print(sum)
All Rights Reserved by Jahirul Opi