[go: up one dir, main page]

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

Homework No.5

Jag suus haguwhh hah

Uploaded by

AJ SAHOTRA
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)
17 views3 pages

Homework No.5

Jag suus haguwhh hah

Uploaded by

AJ SAHOTRA
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

Comp 102 Programming I (Homework No.

5)

FORMAN CHRISTIAN COLLEGE (A Chartered University)


Comp 102 Programming I
Fall 2024
Section: F
Instructor: Adeem Akhtar

Python Functions

Instructions:
1-Rename your file with your name and roll number (e.g., Haroon_25123455.py)
2-Do all your tasks in a single .py file.
3-This is your homework practice for the studied topics that can also help you in
your lab quizzes and assignments as well.
4-Submit your homework on Moodle within the specified deadline.

Task 1:
Write a function that continuously prompts the user to enter numbers until the sum of the
numbers entered reaches or exceeds 100. Skip adding any negative numbers to the sum and
ignore numbers greater than 50. Print the final sum. (use exactly the same function name
given below)

● Input: A series of numbers from the user.


● Output: The final sum of valid numbers.
● fun sum_series():

Task 2:
Write a python function, Inside the function Ask the user for an upper limit n using
a for loop and the range function to find and print all prime numbers from 2 to n. Skip even
numbers after checking 2.

● Input: An upper limit (n) from the user.


● Output: A list of prime numbers up to n.
● fun primes():
Task 3:
Implement python functions to find the sum of the digits of a given number. You will have to
write two functions, one using a for loop and another using a while loop both. Include
comments explaining which one is more efficient and why.

● Input: A number from the user.


● Output: The sum of the digits of the number.
● fun for_sum_of_digits(number):
● fun while_sum_of_digits(number):

Task 4:
Write a python function that takes a string as input and returns the string in reverse.
Implement this using both a for loop and a while loop.

● Input: A list of integers from the user.


● Output: The reversed list.
● fun for_reverse(str):
● fun while_reverse(str):

Task 5:
Write a python function that generates a multiplication table (from 1 to 10) for a number
input by the user. Use the for loop and the range() function. Ask the user if they want the
table to start from 1 or another number, and adjust the range()accordingly.

● Input: A number and a starting point for the table.


● Output: A multiplication table based on the input.
● fun table(num1, num2):

Task 6:
Write a python function that prompts the user to input numbers continuously until they
enter a negative number. The function should sum only the even numbers entered and
ignore the odd numbers. Print the sum when the user enters a negative number.

● Input: A series of numbers from the user.


● Output: The sum of the even numbers.
● fun counti():

Task 7:
Write a function to Ask the user for the number of terms they want in a Fibonacci sequence
and print the sequence using a for loop. Implement the sequence.

● Input: The number of terms for the Fibonacci sequence.


● Output: The Fibonacci sequence up to the specified term.
● fun Fibonacci(num):

Task 8:
Write a function that picks a random number between 1 and 50. Allow the user to guess the
number, but limit the number of guesses to 5 using a for loop. After each incorrect guess, tell
the user if the number is higher or lower. If they guess correctly or run out of attempts,
print an appropriate message.

● Input: A series of guesses from the user.


● Output: Feedback on whether the guess is too high, too low, or correct, along with a
message if attempts run out.
● fun game():

Task 9:

Write a python function that prompts the user for two numbers as the start and end of a
range. The function should find and print all numbers within this range that are both prime
and palindromic. A palindromic number is one that reads the same forwards and backwards
(e.g., 101). Ensure that your function efficiently checks for both conditions using loops and
the range() function.

● Input: Two numbers from the user representing the start and end of the range.
● Output: A list of numbers that are both prime and palindromic.
● fun range_palindrom():

Note: Write Docstring for each function

You might also like