Computer Science
Computer Science
SCIENCE
1 Factorial of a Number
2 Swapping Two Numbers
3 Checking for Prime Number
4 Fibonacci Sequence
5 Sum of Digits of a Number
6 Reversing a Number
7 Checking for Palindrome
8 Finding the Largest Element in a List
9 Counting Occurrences of an Element in a List
10 Simple Interest Calculation
11 Area of a Circle
12 Converting Celsius to Fahrenheit
13 Finding the GCD of Two Numbers
14 Checking for Armstrong Number
15 Generating Multiplication Table
16 Simple Query – Select All Data from a Single Table (SQL)
17 Select Specific Columns (SQL)
18 Join Two Tables – Inner Join (SQL)
19 Aggregate Function – COUNT (SQL)
20 Select with Condition – WHERE Clause (SQL)
21 Connect to MySQL Database and Create a Table
22 Insert Data into a MySQL Table (Python-MySQL)
23 Query Data from a MySQL Table (Python-MySQL)
24 Update Data in a MySQL Table (Python-MySQL)
num = 5
print(f"The factorial of
{num}is{factorial(num)}")
x = 10
y = 20
x, y = swap_numbers(x, y)
print(f"After swapping: x = {x}, y = {y}")
3. Checking for Prime Number
def is_prime(num):
"""Checks if a number is prime."""
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
number = 17
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
4.Fibonacci Sequence
def fibonacci(n):
"""Generates the first n Fibonacci numbers."""
fib_list = []
a, b = 0, 1
for _ in range(n):
fib_list.append(a)
a, b = b, a + b
return fib_list
num_terms = 10
print(f"Fibonacci sequence up to {num_terms}
terms: {fibonacci(num_terms)}")
number = 12345
print(f"The sum of digits of {number} is
{sum_of_digits(number)}")
6. Reversing a Number
def reverse_number(num):
"""Reverses a number."""
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
return reversed_num
number = 1234
print(f"The reverse of {number} is
{reverse_number(number)}")
text = "Racecar"
if is_palindrome(text):
print(f"{text} is a palindrome.")
else:
print(f"{text} is not a palindrome.")
return largest
my_list = [1, 2, 2, 3, 2, 4, 2]
element_to_count = 2
print(f"The element {element_to_count} occurs
{count_occurrences(my_list, element_to_count)}
times.")
def area_of_circle(radius):
"""Calculates the area of a circle."""
area = math.pi * radius**2
return area
radius = 7
print(f"The area of the circle is:
{area_of_circle(radius)}")
celsius_temp = 25
print(f"{celsius_temp} Celsius is equal to
{celsius_to_fahrenheit(celsius_temp)}
Fahrenheit.")
num1 = 48
num2 = 18
print(f"The GCD of {num1} and {num2} is
{gcd(num1, num2)}")
number = 153
if is_armstrong(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong
number.")
Output Table:
employee_id first_name last_name
department_id salary
Output Table:
Output Table:
first_name last_name department_name
John Doe IT
Jane Smith HR
Alice Johnson IT
Bob Brown Marketing
Output Table:
department_id num_employees
101 2
102 1
103 1
condition_result = cursor.fetchall()
print("Condition Result:", condition_result)
Output Table:
first_name last_name salary
conn = mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='test_db'
)
cursor = conn.cursor()
cursor.execute('''
UPDATE Employees
SET salary = salary + 5000
WHERE department_id = 101;
''')
conn.commit()
cursor.close()
conn.close()
print("Salary updated for employees in
department 101.")
THANK YOU