Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Question 1:

Write the functions with the following headers:


# Return the reversal of an integer, e.g. reverse(456) returns # 654
def reverse(number):
# Return true if number is a palindrome
def isPalindrome(number):
Use the reverse function to implement isPalindrome. A number is a palindrome if its reversal is
the same as itself. Write a program that prompts the user to enter an integer and reports whether
the integer is a palindrome.

Answer:

num = int (input ('Enter a number: '))


reverse = int(str(num)[::-1])
print(f"the reverse of number is {reverse}")
if num == reverse:
print("True \n it is a pilondrome")
else:
print("Number is not pilondrome")

Run:

Enter a number: 678

the reverse of number is 876

Number is not pilondrome

Question 2:

Write a function capitalize (lower_case_word) that takes the lower case word and returns the
word with the first letter capitalized. E.g. print (capitalize ('word')) should print the word Word.

Then, given a line of lowercase ASCII words (text separated by a single space), print it with the
first letter of each word capitalized using theyour own function capitalize ().
Input: harry potter Output: Harry Potter

Input: all i see turns to brown Output: All I See Turns To Brow

Answer:

import string
name = input("enter any series of words:")
print(string.capwords(name))

Run:

enter any series of words:abcde

Abcde

Question 3:

Given a non-negative integer n, print the nth Fibonacci number. Do this by writing a


function fib(n) which takes the non-negative integer n and returns the nth Fibonacci number. Use
the concept of recursion to complete fib(n)

Answer:

def Fibonacci(n):
if n < 0:
print("Incorrect input")

elif n == 0:
return 0

elif n == 1:
return 1
else:
return Fibonacci(n - 1) + Fibonacci(n - 2)

n = eval(input("enter any number:"))


print(Fibonacci(n))
Run:

enter any number:6

You might also like