1. Program to Find the Largest Number Among Three Numbers
# Input three numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
# Find the largest number
largest = max(num1, num2, num3)
print("The largest number is:", largest)
Enter first number: 5
Enter second number: 10
Enter third number: 7
The largest number is: 10.0
# Input the interval
start = int(input("Enter the start of the interval: ")) # Example input: 10
end = int(input("Enter the end of the interval: ")) # Example input: 50
print(f"Prime numbers between {start} and {end} are:")
for num in range(start, end + 1):
if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
break
else:
print(num)
Enter the start of the interval: 10
Enter the end of the interval: 50
Prime numbers between 10 and 50 are:
11
13
17
19
23
29
31
37
41
43
47
# Input two numbers
a = float(input("Enter first number: ")) # Example input: 5
b = float(input("Enter second number: ")) # Example input: 10
# Swap the numbers
a = a + b
b = a - b
a = a - b
print("After swapping: First number =", a, "Second number =", b)
Enter first number: 5
Enter second number: 10
After swapping: First number = 10.0 Second number = 5.0
# Input complex numbers
real1 = float(input("Enter real part of first complex number: ")) # Example input: 3
imag1 = float(input("Enter imaginary part of first complex number: ")) # Example input: 2
real2 = float(input("Enter real part of second complex number: ")) # Example input: 1
imag2 = float(input("Enter imaginary part of second complex number: ")) # Example input: 7
# Create complex numbers
complex1 = complex(real1, imag1)
complex2 = complex(real2, imag2)
# Add and multiply
addition = complex1 + complex2
multiplication = complex1 * complex2
print("Addition of complex numbers:", addition)
print("Multiplication of complex numbers:", multiplication)
Enter real part of first complex number: 3
Enter imaginary part of first complex number: 2
Enter real part of second complex number: 1
Enter imaginary part of second complex number: 7
Addition of complex numbers: (4+9j)
Multiplication of complex numbers: (-11+23j)
# Input a number
number = int(input("Enter a number to print its multiplication table: ")) # Example input: 5
print(f"Multiplication table of {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
Enter a number to print its multiplication table: 5
Multiplication table of 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
# Input a number
number = int(input("Enter a number to print its multiplication table: ")) # Example input: 5
print(f"Multiplication table of {number}:")
i = 1
while i <= 10:
print(f"{number} x {i} = {number * i}")
i += 1
Enter a number to print its multiplication table: 5
Multiplication table of 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Here are the Python programs for each of your new questions:
pythondef calculate(a, b):
sum_result = a + b
diff_result = a - b
prod_result = a * b
return sum_result, diff_result, prod_result
# Example usage
x = 10
y = 5
sum_value, diff_value, prod_value = calculate(x, y)
print("Sum:", sum_value)
print("Difference:", diff_value)
print("Product:", prod_value)
Example Output:
makefileSum: 15
Difference: 5
Product: 50
pythondef string_length(s):
count = 0
for char in s:
count += 1
return count
# Example usage
string_input = "Hello, World!"
length = string_length(string_input)
print("Length of the string:", length)
Example Output:
cLength of the string: 13
pythondef greet(name="Guest"):
return f"Hello, {name}!"
# Example usage
print(greet()) # Using default argument
print(greet("Alice")) # Providing a specific argument
Example Output:
Hello, Guest! Hello, Alice!
pythondef check_substring(string, substring):
if substring in string:
return True
else:
return False
# Example usage
main_string = "Hello, World!"
sub_string = "World"
if check_substring(main_string, sub_string):
print(f"'{sub_string}' is present in '{main_string}'")
else:
print(f"'{sub_string}' is not present in '{main_string}'")
Example Output:
csharp'World' is present in 'Hello, World!'
python# Define a list
my_list = [1, 2, 3, 4, 5]
# Addition of two lists
additional_list = [6, 7, 8]
combined_list = my_list + additional_list
print("Combined List:", combined_list)
# Insertion of an element
my_list.insert(2, 99) # Insert 99 at index 2
print("List after insertion:", my_list)
# Slicing the list
sliced_list = my_list[1:4] # Slicing from index 1 to 3
print("Sliced List:", sliced_list)
Example Output:
lessCombined List: [1, 2, 3, 4, 5, 6, 7, 8]
List after insertion: [1, 2, 99, 3, 4, 5]
Sliced List: [2, 99, 3]
Feel free to run these programs in your Python environment, and let me know if you have any questions or need further assistance!
Here are the Python programs for your latest questions regarding dictionaries:
pythondef check_key_exists(dictionary, key):
if key in dictionary:
return True
else:
return False
# Example usage
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
key_to_check = 'age'
if check_key_exists(my_dict, key_to_check):
print(f"Key '{key_to_check}' exists in the dictionary.")
else:
print(f"Key '{key_to_check}' does not exist in the dictionary.")
Example Output:
vbnetKey 'age' exists in the dictionary.
pythondef add_key_value(dictionary, key, value):
dictionary[key] = value
return dictionary
# Example usage
my_dict = {'name': 'Alice', 'age': 25}
new_key = 'city'
new_value = 'Los Angeles'
updated_dict = add_key_value(my_dict, new_key, new_value)
print("Updated Dictionary:", updated_dict)
Example Output:
cssUpdated Dictionary: {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'}
pythondef sum_dictionary_values(dictionary):
total = 0
for value in dictionary.values():
total += value
return total
# Example usage
my_dict = {'item1': 100, 'item2': 200, 'item3': 300}
total_sum = sum_dictionary_values(my_dict)
print("Sum of all items in the dictionary:", total_sum)
Example Output:
cssSum of all items in the dictionary: 600
Feel free to run these programs in your Python environment, and let me know if you have any questions or need further assistance!