Practical File XII
Practical File XII
Practical File
(COMPUTER SCIENCE)
(Subject Code: 083)
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
2. Write a program to sort an array using Bubble Sort and print it.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print("Sorted array is:", arr)
3. Write a program to check the given number is Palindrome or not.
def is_palindrome(s):
return s == s[::-1]
s = input("Enter a string: ")
print("Is palindrome:", is_palindrome(s))
4. Write a Program to print Matrix Multiplication of two Matrix.
def fibonacci(n):
a, b = 0, 1
series = []
while b <= n:
series.append(b)
a, b = b, a + b
return series
num = int(input("Enter a number: "))
print("Fibonacci series up to", num, "is", fibonacci(num))
6. Write a program to check the given number is Prime Number or Not.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
print("Is prime:", is_prime(num))
7. Write a program to check the given number is Armstrong Number or
Not.
def is_armstrong(n):
num_str = str(n)
power = len(num_str)
s = sum(int(digit)**power for digit in num_str)
return s == n
num = int(input("Enter a number: "))
print("Is Armstrong number:", is_armstrong(num))
8. Write a program to calculate Sum of Digits of given number and print it.
def sum_of_digits(n):
return sum(int(digit) for digit in str(n))
num = int(input("Enter a number: "))
print("Sum of digits:", sum_of_digits(num))
9. Write a program to calculate HCF and print it.
def binary_to_decimal(binary):
decimal = 0
for i in range(len(binary)):
decimal += int(binary[i]) * (2**(len(binary)-i-1))
return decimal
binary = input("Enter a binary number: ")
print("Decimal form is:", binary_to_decimal(binary))
12. Write a program Decimal to Binary Conversion and print it.
def decimal_to_binary(n):
return bin(n)[2:]
num = int(input("Enter a decimal number: "))
print("Binary form is:", decimal_to_binary(num))
13. Write a program to Counting Vowels in a String and print count.
def count_vowels(s):
vowels = "aeiouAEIOU"
return sum(1 for char in s if char in vowels)
s = input("Enter a string: ")
print("Number of vowels:", count_vowels(s))
14. Write a program to calculate Simple Interest and print it.
def find_max_min(lst):
return max(lst), min(lst)
lst = [int(x) for x in input("Enter numbers separated by space: ").split()]
max_value, min_value = find_max_min(lst)
print("Maximum value:", max_value, "Minimum value:", min_value)
SQL Query
Sets
1. Write a program to create a table and insert some entries and find Student
marks database
in SQL.
--create table
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Mathematics INT,
Science INT,
English INT
);
-- Create table
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT,
Years_of_Service INT
);
-- Create table
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(100),
Author VARCHAR(50),
Year INT
);
-- Create table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderAmount DECIMAL(10, 2),
OrderDate DATE
);
-- Create table
CREATE TABLE Patients (
PatientID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Disease VARCHAR(50)
);
import mysql.connector
# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)
cursor = conn.cursor()
# Insert a record
query = "INSERT INTO Employees (ID, Name, Department, Salary,
Years_of_Service) VALUES (%s, %s, %s, %s, %s)"
values = (4, 'Sarah', 'Marketing', 55000, 4)
cursor.execute(query, values)
conn.commit()
print(cursor.rowcount, "record inserted.")
import mysql.connector
# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)
cursor = conn.cursor()
# Fetch data
query = "SELECT * FROM Employees"
cursor.execute(query)
result = cursor.fetchall()
# Display data
for row in result:
print(row)
import mysql.connector
# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)
cursor = conn.cursor()
# Update a record
query = "UPDATE Employees SET Salary = %s WHERE ID = %s"
values = (60000, 2)
cursor.execute(query, values)
conn.commit()
print(cursor.rowcount, "record(s) updated.")
import mysql.connector
# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)
cursor = conn.cursor()
# Delete a record
query = "DELETE FROM Employees WHERE ID = %s"
values = (3,)
cursor.execute(query, values)
conn.commit()
print(cursor.rowcount, "record(s) deleted.")