Skip to content

Instantly share code, notes, and snippets.

@i2x
Last active August 8, 2023 11:07
Show Gist options
  • Save i2x/0853eab8244882572c14b85a7a946a1e to your computer and use it in GitHub Desktop.
Save i2x/0853eab8244882572c14b85a7a946a1e to your computer and use it in GitHub Desktop.
LAB5
for num in range(1, 11):
if num % 2 != 0:
continue
print(num)
N = int(input("Enter a positive integer: "))
sum_multiples = 0
for num in range(1, N):
if num % 3 == 0 or num % 5 == 0:
sum_multiples += num
print("\nSum of multiples of 3 and 5 below", N, "is:", sum_multiples)
num = 1
while num <= 15:
if num % 2 == 1 and num % 3 == 0:
break
print(num)
num += 1
def count_even_numbers(numbers):
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
return count
# Test the function
print(count_even_numbers([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) # Output should be 5
print(count_even_numbers([12, 15, 18, 21, 24, 27, 30])) # Output should be 4
def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
# Test the function
print(is_prime(7)) # Output should be True
print(is_prime(12)) # Output should be False
N = int(input("Enter a positive integer: \n"))
for i in range(1, N + 1):
print("*" * i)
N = int(input("Enter the number of elements: "))
sum = 0
for _ in range(N):
num = float(input("\nEnter a number: "))
sum += num
average = sum / N
print("\nAverage:", average)
import random
random.seed(15)
target_number = random.randint(1, 20)
while True:
guess = int(input("Guess the number (between 1 and 20):\n"))
if guess == target_number:
print("Congratulations! You guessed the correct number.")
break
elif guess < target_number:
print("Too low. Try again.")
else:
print("Too high. Try again.")
correct_password = "cprefamily"
while True:
password = input("Enter the password:\n")
if password != correct_password:
print("Incorrect password. Try again.")
continue
else:
print("Access granted!")
break
s = input("Input a string: ")
d=l=0
for c in s:
if c.isdigit():
d=d+1
elif c.isalpha():
l=l+1
else:
pass
print("\nLetters", l)
print("Digits", d)
vowel_count = 0
user_input = input("Enter a string:\n")
for char in user_input:
if char.lower() not in 'aeiou':
continue
vowel_count += 1
print("Number of vowels:", vowel_count)
12)
b.The for loop is used for definite iteration, while the while loop is used for indefinite iteration.
13)
b.15
14)
c.Count: 0 Count: 2 Count: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment