added 7-10

This commit is contained in:
2024-12-07 22:22:21 +02:00
parent 017145b529
commit 94318de22d
4 changed files with 85 additions and 0 deletions

View File

@@ -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

View File

@@ -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}")

View File

@@ -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)

View File

@@ -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