0% found this document useful (0 votes)
34 views3 pages

'Enter A Number: ' F"the Reverse of Number Is " "True It Is A Pilondrome" "Number Is Not Pilondrome"

The document contains three programming questions that require writing Python functions. Question 1 asks to write functions to reverse an integer and check if it is a palindrome. It provides sample input/output. Question 2 asks to write a function to capitalize the first letter of each word in a string. It provides sample input/output. Question 3 asks to write a recursive function to calculate the nth Fibonacci number given a non-negative integer n as input. It provides no sample input/output.

Uploaded by

Simbha Laloo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views3 pages

'Enter A Number: ' F"the Reverse of Number Is " "True It Is A Pilondrome" "Number Is Not Pilondrome"

The document contains three programming questions that require writing Python functions. Question 1 asks to write functions to reverse an integer and check if it is a palindrome. It provides sample input/output. Question 2 asks to write a function to capitalize the first letter of each word in a string. It provides sample input/output. Question 3 asks to write a recursive function to calculate the nth Fibonacci number given a non-negative integer n as input. It provides no sample input/output.

Uploaded by

Simbha Laloo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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