حل لاب2 مادة برمجة غرضية التوجه cs112
- جامعة الامير مقرن
- CS112 Object Oriented Programming - CS112 برمجة غرضية التوجه
- برمجة بايثون | Python programming
- 2025-10-14
توصيف
Please do the following exercises and submit python files during the lab time.
Create a different file for every code.
1. Circle Calculator: Ask the user to enter the radius of a circle.
a. Print the circumference.
b. Print the area (rounded to 2 decimals). � � Hint: Circumference = 2 × π × r, Area = π × r²
2. Simple Grade Checker: Ask the user for a mark (0–100).
a. If ≥ 90 → "Excellent"
b. If ≥ 70 → "Good"
c. If ≥ 50 → "Pass" d. Else → "Fail"
grade=int(input("enter your grade (0-100)"))
if grade>100 or grade<0:
print("error")
elif grade>=90:
print("grade excellent")
elif grade>=70:
print("good")
elif grade>=50:
print("pass")
else:
print("fail")
3. Sum of Odd Numbers: Ask the user for a number N. Print the sum of all odd numbers from 1 to N.
num=int(input("enter a number"))
t=0
for number in range(0,num):
if number %2!=0:
t=t+number
print("t number is t")
print(t)*
4. Reverse Words: Ask the user for a sentence. Print each word reversed, but keep the word order. Example: "hello world" → "olleh dlrow"
sentence = input("Enter a sentence: ")
words = sentence.split() # Split into words
reversed_words = [word[::-1] for word in words] # Reverse each word
print(" ".join(reversed_words)) # Join back with spaces
5. Guess the Secret Number: Set a secret number =
a. Ask the user to keep guessing until correct. Print "Correct!" when guessed.
secret = "11"
guess = input("Guess the secret number : ")
attempts = 0
while guess != secret:
print("Wrong! Try again.")
guess = input("Guess again: ")
attempts+=1
print(f"Correct! It took {attempts} attempts.")
6. Student List Manager: Create an empty list.
a. Ask the user to enter 5 student names.
b. Print the list.
c. Print the list sorted alphabetically.
students = []
for i in range(5):
name = input(f"Enter student {i+1} name: ")
students.append(name)
students.sort()
print("Student list:", students)
7. Multiples of a Number: Ask the user for a number. Print its multiples up to 50. Example: if n=7 → 7, 14, 21, 28, 35, 42, 49.
num = int(input("Enter a number: "))
multiples=num
while (multiples+7)<=50:
multiples=multiples+num
print(f"Multiples of {num} up to 50: {multiples}")
8. Palindrome Word: Ask the user to enter a word. Check if it is a palindrome (reads the same forward and backward). Examples: level, madam, racecar, civic, rotor
word = input("Enter a word: ").lower().replace(" ", "")
if word == word[::-1]: # Compare with reverse
print("It's a palindrome!")
else: print("Not a palindrome.")
9. Replace Vowels: Ask the user for a sentence. Replace all vowels (a, e, i, o, u) with "*".
sentence = input("Enter a sentence: ")
vowels = "aeiouAEIOU"
for v in vowels:
sentence=sentence.replace(v,"*")
print("Vowels replaced (removed):", sentence)
10. Simple Calculator (Loop Version): Write a calculator that:
a. Asks for two numbers.
b. Asks for an operation (+, -, *, /).
c. Prints the result.
d. Repeats until the user types "exit".
while True:
num1=int(input("enter number1: "))
num2=int(input("enter number2: "))
operation=input("enter operation: ")
if operation=='+':
result=num1+num2
elif operation=='-':
result=num1-num2
elif operation=='*':
result=num1*num2
elif operation=='/':
result=num1/num2
elif operation=="exit":
break
else: result="invalid operation"
print(result)