From 94318de22dd9d09a7ab05f564bafce9cf1c7389b Mon Sep 17 00:00:00 2001 From: tom Date: Sat, 7 Dec 2024 22:22:21 +0200 Subject: [PATCH] added 7-10 --- basic programming/2nd task/10.py | 15 +++++++++++++++ basic programming/2nd task/7.py | 21 ++++++++++++++++++++ basic programming/2nd task/8.py | 33 ++++++++++++++++++++++++++++++++ basic programming/2nd task/9.py | 16 ++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 basic programming/2nd task/10.py create mode 100644 basic programming/2nd task/7.py create mode 100644 basic programming/2nd task/8.py create mode 100644 basic programming/2nd task/9.py diff --git a/basic programming/2nd task/10.py b/basic programming/2nd task/10.py new file mode 100644 index 0000000..e99080e --- /dev/null +++ b/basic programming/2nd task/10.py @@ -0,0 +1,15 @@ +# Q10 + +# Receive input from the user +n = int(input("Enter a natural number (n): ")) + +# Variable to store the current number +current_number = 1 + +# Outer loop to print according to the number of rows +for row in range(1, n + 1): + # Inner loop to print the numbers for the current row + for col in range(row): + print(current_number, end=" ") + current_number += 1 # Increment the number + print() # Move to the next line after finishing the current row diff --git a/basic programming/2nd task/7.py b/basic programming/2nd task/7.py new file mode 100644 index 0000000..2d27425 --- /dev/null +++ b/basic programming/2nd task/7.py @@ -0,0 +1,21 @@ +# Q7 + +# Initiallize variables +count = 0 +numbers_per_line = 10 +numbers_in_row = 0 + +for num in range(1000, 3001): + if num % 2 == 0 and num % 3 == 0 and num % 5 == 0: + print(num, end=' ') + count += 1 + numbers_in_row += 1 + + if numbers_in_row == numbers_per_line: + print() + numbers_in_row = 0 + +if numbers_in_row > 0: + print() + +print(f"Total numbers that are divisible by 2, 3, and 5: {count}") diff --git a/basic programming/2nd task/8.py b/basic programming/2nd task/8.py new file mode 100644 index 0000000..cc5549c --- /dev/null +++ b/basic programming/2nd task/8.py @@ -0,0 +1,33 @@ +# Q8 + +# Imports +import math + +# Receiving input from the user +n = int(input("Enter the first number (n): ")) +m = int(input("Enter the second number (m): ")) + +# Ensure n is greater than m by swapping if necessary +if m > n: + n, m = m, n # Swap the values of n and m + +# Variables for prime numbers and their sum +prime_numbers = [] +sum_primes = 0 + +# Iterate through the range from m to n +for num in range(m, n+1): + if num > 1: # Only check numbers greater than 1 + is_prime = True # Assume the number is prime + for i in range(2, int(math.sqrt(num)) + 1): # Check divisibility up to the square root of num + if num % i == 0: + is_prime = False # The number is not prime + break + + if is_prime: + prime_numbers.append(num) + sum_primes += num + +# Print the results +print("Prime numbers in the range:", prime_numbers) +print("Sum of prime numbers:", sum_primes) diff --git a/basic programming/2nd task/9.py b/basic programming/2nd task/9.py new file mode 100644 index 0000000..d81b94f --- /dev/null +++ b/basic programming/2nd task/9.py @@ -0,0 +1,16 @@ +# Q9 + +# Initialize the variables for the first two numbers in the Fibonacci sequence +a = 0 # The first number +b = 1 # The second number + +# Print the first two numbers +print(a, end=" ") +print(b, end=" ") + +# Calculate and print the next numbers in the Fibonacci sequence +for _ in range(8): # We have already printed the first two numbers, so we need 8 more + c = a + b # The next number is the sum of the previous two numbers + print(c, end=" ") + a = b # Move the values: a gets the value of b + b = c # b gets the value of c