0% found this document useful (0 votes)
13 views

Lab_manual computer science class 12th

Lab manual pdf class 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab_manual computer science class 12th

Lab manual pdf class 12th
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab manual

Program Statement 1: Write a program to print "Hello, World!" to the screen.


Source code-
print("Hello, World!")
Output-
Hello, World!
Program Statement 2: Write a program to take two numbers as input and display their sum.
Source code-
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)
Output-
Enter first number: 5
Enter second number: 10
Sum: 15
Program Statement 3: Write a program to check whether a given number is even or odd.
Source code-
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
Output-
Enter a number: 7
Odd
Program Statement 4: Write a program to find the largest of three input numbers.
Source code-
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Largest:", max(a, b, c))
Output-
Enter first number: 10
Enter second number: 25
Enter third number: 15
Largest: 25
Program Statement 5: Write a program to print the multiplication table for a given number.
Source code-
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Output-
Enter a number: 3
3x1=3
3x2=6
3x3=9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Program Statement 6: Write a program to calculate the factorial of a given number.
Source code-
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("Factorial:", factorial)
Output-
Enter a number: 5
Factorial: 120
Program Statement 7: Write a program to check whether a number is prime or not.
Source code-
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")
Outcome-
Enter a number: 13
Prime
Enter a number: 20
Not Prime
Program Statement 8: Write a program to display the Fibonacci sequence up to 'n' terms.
Source code-
n = int(input("Enter the number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Output-
Enter the number of terms: 5
01123
Program Statement 9: Write a program to reverse a given string.
Source code-
text = input("Enter a string: ")
print("Reversed:", text[::-1])
Output-
Enter a string: python
Reversed: nohtyp
Program Statement 10: Write a program to count the number of vowels in a given string.
Source code-
text = input("Enter a string: ").lower()
vowels = "aeiou"
count = sum(1 for char in text if char in vowels)
print("Number of vowels:", count)
Output-
Enter a string: OpenAI
Number of vowels: 3
Program Statement 11: Write a program to check whether a given string is a palindrome.
Source code-
text = input("Enter a string: ").lower()
if text == text[::-1]:
print("Palindrome")
else:
print("Not a palindrome")
Output-
Enter a string: madam
Palindrome
Enter a string: hello
Not a palindrome
Program Statement 12: Write a program to find the sum of the digits of a given number.
Source code-
num = int(input("Enter a number: "))
total = sum(int(digit) for digit in str(num))
print("Sum of digits:", total)
Output-
Enter a number: 1234
Sum of digits: 10
Program Statement 13: Write a program to generate a random number between 1 and 100.
Source code-
import random
print("Random number between 1 and 100:", random.randint(1, 100))
Output-
Random number between 1 and 100: 57
Program Statement 14: Write a program to convert a temperature from Celsius to Fahrenheit.
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is {fahrenheit}°F")
Output-
Enter temperature in Celsius: 25
25.0°C is 77.0°F
Program Statement 15: Write a program to check whether a given number is a perfect square.
Source code-
import math
num = int(input("Enter a number: "))
if math.isqrt(num) ** 2 == num:
print("Perfect square")
else:
print("Not a perfect square")
Output-
Enter a number: 16
Perfect square
Enter a number: 20
Not a perfect square
Program statement 17- Create a table named Students with appropriate columns and insert the given sample
data into the table.
Source code-
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT,
Class VARCHAR(10),
Marks INT
);
INSERT SAMPLE DATA
INSERT INTO Students (StudentID, Name, Age, Class, Marks)
VALUES
(1, 'John', 14, '8th', 85),
(2, 'Alice', 13, '7th', 78),
(3, 'Bob', 14, '8th', 92),
(4, 'Daisy', 13, '7th', 88),
(5, 'Tom', 15, '9th', 75);
*DRAW THIS TABLE
Basic Queries
1. Display all records
SELECT * FROM Students;

2. Find students older than 13 years


SELECT * FROM Students WHERE Age > 13;

3. Retrieve students in the 8th class


SELECT Name, Marks FROM Students WHERE Class = '8th';
4. Sort students by marks in descending order
SELECT * FROM Students ORDER BY Marks DESC;

5. Find the average marks of all students


SELECT AVG(Marks) AS AverageMarks FROM Students;

Program statement 18 - Create a relationship between two tables (e.g., Students and Courses) and write
queries that involve both tables.
Source code-
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(50),
Teacher VARCHAR(50)
);

CREATE TABLE Students (


StudentID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Class VARCHAR(10),
CourseID INT,
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
);
INSERT SAMPLE DATA
INSERT INTO Courses (CourseID, CourseName, Teacher)
VALUES
(1, 'Math', 'Mr. Smith'),
(2, 'Science', 'Ms. Johnson');

INSERT INTO Students (StudentID, Name, Age, Class, CourseID)


VALUES
(1, 'John', 14, '8th', 1),
(2, 'Alice', 13, '7th', 2);

Basic queries
1. Retrieve the names of students along with their course teacher
SELECT Students.Name AS StudentName, Courses.CourseName, Courses.Teacher
FROM Students
JOIN Courses ON Students.CourseID = Courses.CourseID;

2. Find the total number of students in each course


SELECT Courses.CourseName, COUNT(Students.StudentID) AS TotalStudents
FROM Courses
LEFT JOIN Students ON Courses.CourseID = Students.CourseID
GROUP BY Courses.CourseName;

3. List all students who are not enrolled in any course


SELECT Name AS StudentName
FROM Students
WHERE CourseID IS NULL;

4. Find courses with no students enrolled


SELECT Courses.CourseName
FROM Courses
LEFT JOIN Students ON Courses.CourseID = Students.CourseID
WHERE Students.StudentID IS NULL;

5. Retrieve the average age of students in each course


SELECT Courses.CourseName, AVG(Students.Age) AS AverageAge
FROM Courses
JOIN Students ON Courses.CourseID = Students.CourseID
GROUP BY Courses.CourseName;

You might also like