0% found this document useful (0 votes)
50 views7 pages

Lab 5

This document contains Python code snippets to demonstrate the use of loops like for loops and while loops. It includes 23 examples of programs that use loops to print patterns, find sums, check palindromes, and more. The examples cover basic to advanced uses of loops in Python programming.

Uploaded by

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

Lab 5

This document contains Python code snippets to demonstrate the use of loops like for loops and while loops. It includes 23 examples of programs that use loops to print patterns, find sums, check palindromes, and more. The examples cover basic to advanced uses of loops in Python programming.

Uploaded by

Amr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

================= Lab 5 ================

#==================== Lab 5 ========================

print(" Q5. Do these Programming Exercises with Python language to:")

#print('========= lab5 Q.5 (1) =========\n')


# #1. Create a loop that counts from 0 to 100
#
# for i in range(101): # Or for i in range(0,101,1):
# print(i,end=" ")
#
# print("=============================================")

#print('========= lab5 Q.5 (2) =========\n')


# # 2. What's the difference between a while loop and a for loop?
#
# #=================== while loop =================
# num = 0
# while num < 5:
# print(num)
# num += 1
# #=================== for loop =================
# fruits = ["apple", "banana", "cherry"]
# for fruit in fruits:
# print(fruit)
# print("=============================================")

#print('========= lab5 Q.5 (3) =========\n')


# # 3. Write a Python program to print all even numbers between 1 to 100 using while
loop.
#
# #============= ‫=============== طريقة‬
# number = 0
# while number < 101:
# if number % 2 == 0:
# print(number, end=" ")
# number += 1
# print()
# #============= ‫=============== طريقة‬
# print()
# number = 0
# while number < 101:
# print(number, end=" ")
# number += 2
#
# print("=============================================")

#print('========= lab5 Q.5 (4) =========\n')


# # 4. Write a Python program to find the sum of first 10 natural numbers using for loop.
#
# sum = 0
# for num in range(1,11):
# sum += num
# print("The sum of the numbers from 1 to 10 is : (",sum,") .")
#
# print("=============================================")

‫الم‬
‫ رقنة سلمان و سدى الورد‬/ ‫هندسيان‬ ‫ ضياء احمد علي الش فري‬/ ‫الطالب‬
================= Lab 5 ================

#print('========= lab5 Q.5 (5) =========\n')


# # 5. Write a Python Program to Reverse a Given Number
#
# number = int(input("Enter the number: "))
# reversed_number = str(number)[::-1]
#
# print("The reversed number is:", int(reversed_number))
#
# print("=============================================")

#print('========= lab5 Q.5 (6) =========\n')


# #6. Write a Python program takes in a number and finds the sum of digits in a number.
#
# #========================= ‫مفهوم غير‬
# number = int(input("Enter a number: "))
# sum_of_digits = 0
#
# while number > 0:
# digit = number % 10
# sum_of_digits += digit
# number //= 10
#
# print("The sum of the digits in the number is:", sum_of_digits)
# print("=============================================")

#print('========= lab5 Q.5 (7) =========\n')


# # 7. Write a Python program that takes a number and checks whether it is a
palindrome or not.
#
# # ================= ‫مفهوم غير‬
# number = int(input("Enter a number: "))
# original_number = number
# reversed_number = 0
#
# while number > 0:
# digit = number % 10
# reversed_number = (reversed_number * 10) + digit
# number //= 10
#
# if original_number == reversed_number:
# print("The number is a palindrome")
# else:
# print("The number is not a palindrome")
#
# print("=============================================")

#print('========= lab5 Q.5 (8) =========\n')


# # 8. Make a multiplication table using a loop
#
# for i in range(1,13):
# for j in range(1,13):
# print(j, " * ", i, " = ", i * j)
# print()
#
# print("=============================================")

#print('========= lab5 Q.5 (9) =========\n')


# # 9. Output the numbers 1 to 10 backwards using a loop
#
# for num in range(10,0,-1):

‫الم‬
‫ رقنة سلمان و سدى الورد‬/ ‫هندسيان‬ ‫ ضياء احمد علي الش فري‬/ ‫الطالب‬
================= Lab 5 ================

# print(num,end=" ")
#
# print("=============================================")

#print('========= lab5 Q.5 (10) =========\n')


# # 10. Create a loop that counts all even numbers to 10
#
# sum = 0
# for num in range(11):
# if num % 2 == 0:
# sum += num
# print("The counts all even numbers to 10 is : (",sum,"). ")
#
# print("=============================================")

#print('========= lab5 Q.5 (11) =========\n')


# # 11. Create a loop that sums the numbers from 100 to 200
#
# sum = 0
# for num in range(100,201):
# sum += num
# print("The sum of the numbers between 100 and 200 is : (",sum,"). ")
#
# print("=============================================")

#print('========= lab5 Q.5 (12) =========\n')


# # 12. Print First 10 natural numbers using while loop
#
# num = 1
# while num < 11:
# print(num,end=" , ")
# num += 1
#
# print("=============================================")

#print('========= lab5 Q.5 (13) =========\n')


# # 13. Write a program to print the following number pattern using a loop.
# # 1
# # 2 2
# # 3 3 3
# # 4 4 4 4
# # 5 5 5 5 5
#
# for i in range(6):
# for j in range(i):
# print(i, end=" ")
# print()
#
# print("=============================================")

#print('========= lab5 Q.5 (14) =========\n')


# # 14. Write a program to accept a number from a user and calculate the sum of all
numbers
# # from 1 to a given number. For example, if the user entered 10the output
# # should be 55 (1+2+3+4+5+6+7+8+9+10)
#
# num = int(input("Enter the number you need stop : "))
# sum = 0
# for i in range(1, num + 1):
# sum += i
# print("The sum of the numbers from 1 to num is : (",sum,") .")

‫الم‬
‫ رقنة سلمان و سدى الورد‬/ ‫هندسيان‬ ‫ ضياء احمد علي الش فري‬/ ‫الطالب‬
================= Lab 5 ================

#
# print("=============================================")

#print('========= lab5 Q.5 (15) =========\n')


# # 15. Write a program to print multiplication table of a given number For example, num =
2 so
# # the output should be
#
# num = int(input("Enter the number you want to get the multiplication table : "))
# num_end = int(input("Enter the number you want to stop at : "))
#
# for i in range(1, num_end + 1):
# print(num, " * ", i, " = ", num * i)
#
#print("=============================================")

#print('========= lab5 Q.5 (16) =========\n')


# #16. Write a program to count the total number of digits in a number using a while loop.
# # For example, the number is 75869, so the output should be 5.
#
# num = int(input("Enter the numbers : "))
# count = 0
#
# while num > 0:
# num //= 10
# count += 1
#
# print("The total number of digits in the number is:", count)
#
# print("=============================================")

#print('========= lab5 Q.5 (17) =========\n')


# #17. Write a program to use for loop to print the following reverse number pattern
#
# rows = int(input("Enter the num rows : "))
#
# for i in range(rows, 0, -1):
# for j in range(i, 0, -1):
# print(j, end=" ")
# print()
#
# print("=============================================")

#print('========= lab5 Q.5 (18) =========\n')


# #18. Display numbers from -10 to -1 using for loop
#
# for num in range(-10,0,1):
# print(num, end=" , ")
#
# print("=============================================")

#print('========= lab5 Q.5 (19) =========\n')


# #19. Use else block to display a message ''Done" after successful execution of for loop
For
# # example, the following loop will execute without any error.
#
# for i in range(5):
# print("Num = ",i)
# else:
# print("Done...")
#

‫الم‬
‫ رقنة سلمان و سدى الورد‬/ ‫هندسيان‬ ‫ ضياء احمد علي الش فري‬/ ‫الطالب‬
================= Lab 5 ================

# print("=============================================")

#print('========= lab5 Q.5 (20) =========\n')


# # 20. Write a program to display all prime numbers within a rang
#
# start = int(input("Enter the start number for the scan : "))
# end = int(input("Enter the end number for the scan : "))
#
# print("Prime numbers in the range from", start, "to", end, "are:")
#
# for num in range(start, end + 1):
# if num > 1:
# for i in range(2, num):
# if (num % i) == 0:
# break
# else:
# print(num)
#
# print("=============================================")

#print('========= lab5 Q.5 (21) =========\n')


# # 21.Display Fibonacci series up to 10 terms. The Fibonacci Sequence is a
series of
# # numbers. The next number is found by adding up the two numbers before it.
# # The first two numbers are 0 and 1. For example, 0,1,1, 2, 3, 5, 8,13, 21. The
# # next number in this series above is 13+21 = 34.
#
# # ‫مفهوم غير‬
# num1 = 0
# num2 = 1
#
# print(num1)
# print(num2)
#
# for i in range(8):
# num3 = num1 + num2
# print(num3)
# num1 = num2
# num2 = num3
# print("=============================================")

#print('========= lab5 Q.5 (22) =========\n')


# # 22. Write a program to use the loop to find the factorial of a given number.
#
# number = int(input("Enter a number: "))
#
# temp = 1
#
# for i in range(1, number + 1):
# temp *= i
#
# print("The factorial of", number, "is (", temp, ").")
#
# print("=============================================")

# print('========= lab5 Q.5 (23) =========\n')


# #23.Write a program to rint the cube of all numbers from 1 to a given number
#
# number = int(input("Enter a number: "))
#
# for i in range(1, number + 1):

‫الم‬
‫ رقنة سلمان و سدى الورد‬/ ‫هندسيان‬ ‫ ضياء احمد علي الش فري‬/ ‫الطالب‬
================= Lab 5 ================

# cube = i ** 3
# print("Cube of ", i, "is (", cube, ").")
#
# print("=============================================")

#print('========= lab5 Q.5 (24) =========\n')


# #24.Write a program to calculate the sum of series up to n term. For example, if n
=5 the
# # series will become 2 + 22 + 222 + 2222 + 22222 = 24690
#
# num = int(input("Enter the value of number : "))
#
# term = 2
# series_sum = 0
#
# for i in range(num):
# series_sum += term
# term = term * 10 + 2
#
# print("The sum of the series is:", series_sum)
#
# print("=============================================")

#print('========= lab5 Q.5 (25) =========\n')


# # 25. Write a program to print the following start pattern using the for loop
#
# rows = int(input("Enter the number of rows: "))
#
# for i in range(rows):
# for j in range(i + 1):
# print("*", end=" ")
# print()
#
# print("=============================================")

#print('========= lab5 Q.5 (26) =========\n')


# # 26. Print the following pattern
# i = int(input("Enter the number : "))
# for i in range(i + 1):
# for j in range(i):
# print(i, end=" ")
# print()
#
# print("=============================================")

#print('========= lab5 Q.5 (27) =========\n')


# # 27. Print multiplication table from 1 to 10
#
# for i in range(1,11):
# for j in range(1,11):
# print(j, " * ", i, " = ", i * j)
# print()
#
# print("=============================================")

#print('========= lab5 Q.5 (28) =========\n')


# # 28. Print a downward Half-Pyramid Pattern of Star (asterisk)
#
# rows = int(input("Enter the number of rows: "))
#

‫الم‬
‫ رقنة سلمان و سدى الورد‬/ ‫هندسيان‬ ‫ ضياء احمد علي الش فري‬/ ‫الطالب‬
================= Lab 5 ================

# for i in range(rows, 0, -1):


# for j in range(i):
# print("*", end=" ")
# print()
#
# print("=============================================")

#print('========= lab5 Q.5 (29) =========\n')


# # 29. Given a tic-tac-toe board of 3x3, print every position by Nested loops
#
# # ‫يؤجـــــل‬
# print("=============================================")

#print('========= lab5 Q.5 (30) =========\n')


# #30. Write a python program to print all the months of given year.
#
# import calendar
#
# print("The months of the year are : ")
# for i in range(1, 13):
# month_name = calendar.month_name[i]
# print(i," : ",month_name)
#
# print("=============================================")

#print('========= lab5 Q.5 (31) =========\n')


# # 31. write a python program to reverse a string without using any built in function
#
#
#
# # ‫السؤال غير مفهوم‬
#
#
#
#
#
# print("=============================================")

#print('========= lab5 Q.5 (32) =========\n')


# # 32.Write a program using a while loop that asks the user for a number, and
prints a
# # countdown from that number to zero.
#
# num = int(input("Enter the number : "))
# while num >= 0:
# print(num, end=" ")
# num -= 1
#
# print("=============================================")

#print('========= lab5 Q.5 (33) =========\n')


# # 33. Using a for loop, write a program that prints the decimal equivalents of
1/2,1/3,1/4 ,... 1/10
#
# for i in range(2, 11):
# decimal = 1 / i
# print(f"1/{i} = {decimal}")
#
# print("=============================================")

‫الم‬
‫ رقنة سلمان و سدى الورد‬/ ‫هندسيان‬ ‫ ضياء احمد علي الش فري‬/ ‫الطالب‬

You might also like