Sec - Program Document
Sec - Program Document
PROGRAM NO - 1
TO CHECK WHETHER A NUMBER IS PRIME OR NOT
CODE -
import math
def is_prime(n):
if n <= 1:
return False
if n == 2:
return True # 2 is prime
if n % 2 == 0:
return False # Even numbers other than 2 are not prime
for i in range(3, int(math.sqrt(n)) + 1, 2): # Only check odd numbers
if n % i == 0:
return False
return True
# Example usage
print(is_prime(29)) # True
print(is_prime(30)) # False
OUTPUT : -
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC
v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
PROGRAM NO - 2
TO LIST PRIME NUMBER UPTO 1000
CODE -
def sieve_of_eratosthenes(limit):
# Create a boolean array "prime[0..limit]" and initialize all entries as True
prime = [True] * (limit + 1)
prime[0] = prime[1] = False # 0 and 1 are not prime
# Example usage
primes_up_to_1000 = sieve_of_eratosthenes(1000)
print(primes_up_to_1000)
OUTPUT:-
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
PROGRAM NO - 3
TO CALCULATE AREA OF RECTANGLE
CODE :-
# Example usage
length = 5 # You can change this value
width = 3 # You can change this value
area = area_of_rectangle(length, width)
print(f"The area of the rectangle is {area} square units.")
OUTPUT:-
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC
v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
PROGRAM NO - 4
TO CHECK SIZE OF VARIABLES IN BYTES (USE OF SIZE OF()
OPERATOR)
CODE:-
import sys
# Example variables
int_var = 42
float_var = 3.14159
str_var = "Hello, Python!"
list_var = [1, 2, 3, 4, 5]
dict_var = {"a": 1, "b": 2, "c": 3}
OUTPUT:-
PROGRAM NO - 5
TO FIND LARGEST OF THREE NUMBERS
CODE :-
def largest_of_three(a, b, c):
return max(a, b, c)
# Example usage
num1 = 10
num2 = 25
num3 = 15
largest = largest_of_three(num1, num2, num3)
print(f"The largest number is: {largest}")
OUTPUT:-
============ RESTART: C:/Users/ramba/Pictures/sec python/RITISH 5.py
===========
The largest number is: 25
SEC : PROGRAMMING WITH PYTHON
PROGRAM NO - 6
TO FINDS ROOTS OF QUADRATIC EQUATION
CODE :-
import cmath # For handling complex roots
# Example usage
a=1
b = -3
c=2
OUTPUT:-
============ RESTART: C:/Users/ramba/Pictures/sec python/RITISH 6.py
===========
The roots of the quadratic equation are: (2+0j) and (1+0j)
SEC : PROGRAMMING WITH PYTHON
PROGRAM NO - 7
CONVERTING PLANE POLAR TO CARTESIAN COORDINATES
AND VICE VERSA.
CODE :-
import math
# Example usage
r = 5 # Radial distance
theta = math.radians(30) # Convert angle in degrees to radians (30°)
x, y = polar_to_cartesian(r, theta)
print(f"Polar to Cartesian: ({r}, 30°) -> Cartesian: ({x}, {y})")
import math
# Example usage
x=3
y=4
r, theta = cartesian_to_polar(x, y)
print(f"Cartesian to Polar: ({x}, {y}) -> Polar: ({r}, {theta}°)")
OUTPUT:-
============ RESTART: C:/Users/ramba/Pictures/sec python/RITISH 4.py
===========
Polar to Cartesian: (5, 30°) -> Cartesian: (4.330127018922194,
2.4999999999999996)
Cartesian to Polar: (3, 4) -> Polar: (5.0, 53.13010235415598°)