COMPUTER PRACTICAL FILE
NAME- ASMITA GHOSH
SCHOOL- TECHNO INDIA GROUP PUBLIC
SCHOOL
CLASS- XI SECTION- SCIENCE 4
ROLL NO.-13
SUBJECT- COMPUTER SCIENCE
SESSION – 2023-2024
1) Python Program to Find the Factorial of a
Number num = int(input("Enter a number: "))
factorial = 1 for i in range(1, num + 1):
factorial *= i print("The factorial of
{num} is {factorial}")
OUTPUT
Enter a number: 5
The factorial of 5 is 120
2)Python Program checking if a given number is
Fibonacci number
num = int(input("Enter a number: "))
a, b = 0, 1
while a < num:
a, b = b, a + b
if a == num:
print("{num} is a Fibonacci number.")
else:
print("{num} is not a Fibonacci number.")
OUTPUT
Enter a number: 8
8 is a Fibonacci number.
3)Calculate the Area of a Triangle
base = float(input("Enter the base of the triangle:
")) height = float(input("Enter the height of the
triangle: ")) area = 0.5 * base * height if base >=
0 and height >= 0:
print("The area of the triangle is
{area}") else:
print("Invalid input. Base and height must be non
negative.")
OUTPUT
Enter the base of the triangle: 5
Enter the height of the triangle: 8
The area of the triangle is 20.00
4)Check if a Number is Even or Odd
num = int(input("Enter a number: "))
if num % 2 == 0:
print("{num} is even.")
else:
print("{num} is odd.")
OUTPUT
Enter a number: 12
12 is even.
5)Determine if a Year is a Leap Year
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year %
400 == 0): print("{year} is a leap year.")
else :
print("{year} is not a leap year.")
OUTPUT
Enter a year: 2024
2024 is a leap year.
6)Printing Numbers in a Loop
for i in range(1, 6):
print(i)
OUTPUT:
1
2
3
4
5
7)Printing a Pattern (Right
Triangle) n = 5 for i in range(1, n
+ 1): for j in range(i):
print("*", end=" ")
print()
OUTPUT:
*
**
***
****
*****
8) While Loop to Find the Sum of Natural
Numbers
n = 5 sum
=0
i = 1 while i
<= n:
sum += i
i += 1
print("The sum of the first {n} natural numbers is
{sum}")
OUTPUT:
The sum of the first 5 natural numbers is 15
8) Counting Vowels in a String
string = "Hello, World!"
vowels = "AEIOUaeiou"
count = 0
for char in string: if char in vowels:
count += 1 print("The number of vowels in the
string is {count}")
OUTPUT:
The number of vowels in the string is 3
9) Printing a Pattern (Inverted Right
Triangle) n=5
for i in range(n, 0, -1):
for j in range(i):
print("*", end=" ")
print()
OUTPUT:
** * * *
** * *
** *
**
*
10)Reversing a String
string = "Python"
reversed_string = ""
for char in string:
reversed_string = char + reversed_string
print("The reversed string is: {reversed_string}")
Output:
The reversed string is: nohtyP
11) Sum of Natural Numbers
upto a given number
num = int(input("Enter a positive integer: "))
sum = 0
if num < 1:
print("Please enter a positive integer.")
else:
for i in range(1, num + 1):
sum += i
print("The sum of natural numbers from 1 to
{num} is
{sum}")
OUTPUT
Enter a positive integer: 5
The sum of natural numbers from 1 to 5 is 15
12) Program to check if a number is prime
num = int(input("Enter a number: "))
is_prime = True
if num < 2:
is_prime = False
else:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(f"{num} is a prime number.")
else:
print("{num} is not a prime number.")
OUTPUT
Enter a number: 17
17 is a prime number.