fbpx

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)

				
			

Out Put :-

				
					Enter first number: 5
Enter second number: 10
Enter third number: 7
The largest number is: 10.0

				
			

2. Program to Display All Prime Numbers Within an Interval

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

				
			

Out Put :-

				
					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

				
			

3. Program to Swap Two Numbers Without Using a Temporary Variable

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

				
			

Out Put :-

				
					Enter first number: 5
Enter second number: 10
After swapping: First number = 10.0 Second number = 5.0

				
			

4. Program to Add and Multiply Complex Numbers

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

				
			

Out Put :-

				
					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)

				
			

5. Program to Print Multiplication Table of a Given Number
a.) Using For Loop

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

				
			

Out Put :-

				
					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

				
			

b.) Using While Loop

				
					# 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

				
			

Out Put :-

				
					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

				
			

2.1

ChatGPT

Here are the Python programs for each of your new questions:

1. Program to Define Multiple Return Values

python
def 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:

makefile
Sum: 15 Difference: 5 Product: 50

2. Program to Find the Length of a String Without Any Library Function

python
def 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:

c
Length of the string: 13

3. Program to Define a Function with Default Argument (Calling Functions Without Keyword Arguments)

python
def 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!

4. Program to Check if a Substring is Present in a Given String

python
def 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!'

5. Program to Perform Operations on a List (Addition, Insertion, Slicing)

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:

less
Combined 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!

3.1

ChatGPT

Here are the Python programs for your latest questions regarding dictionaries:

1. Program to Check if a Given Key Exists in a Dictionary

python
def 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:

vbnet
Key 'age' exists in the dictionary.

2. Program to Add a New Key-Value Pair to an Existing Dictionary

python
def 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:

css
Updated Dictionary: {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'}

3. Program to Sum All the Values in a Given Dictionary

python
def 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:

css
Sum 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!

Scroll to Top