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

Sec - Program Document

Uploaded by

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

Sec - Program Document

Uploaded by

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

SEC : PROGRAMMING WITH PYTHON

NAME - RITISH KUMAR MAHTO


COURSE - BSC PHYSICS HONOURS
ROLL NO - PHH24095
COLLEGE - MOTILAL NEHRU COLLEGE (UNIVERSITY OF DELHI )

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.

= RESTART: C:/Users/ramba/Pictures/sec python/Ritish.py


True
False
SEC : PROGRAMMING WITH PYTHON

NAME - RITISH KUMAR MAHTO


COURSE - BSC PHYSICS HONOURS
ROLL NO - PHH24095
COLLEGE - MOTILAL NEHRU COLLEGE (UNIVERSITY OF DELHI )

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

# Sieve of Eratosthenes algorithm


p=2
while p * p <= limit:
if prime[p]:
# Marking multiples of p as False
for i in range(p * p, limit + 1, p):
prime[i] = False
p += 1

# Collecting and returning all prime numbers


primes = [i for i in range(limit + 1) if prime[i]]
return primes

# 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.

============ RESTART: C:/Users/ramba/Pictures/sec python/RITISH 2.py ===========


[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113,
127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241,
251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523,
541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673,
677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
SEC : PROGRAMMING WITH PYTHON

NAME - RITISH KUMAR MAHTO


COURSE - BSC PHYSICS HONOURS
ROLL NO - PHH24095
COLLEGE - MOTILAL NEHRU COLLEGE (UNIVERSITY OF DELHI )

PROGRAM NO - 3
TO CALCULATE AREA OF RECTANGLE

CODE :-

def area_of_rectangle(length, width):


return length * width

# 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.

= RESTART: C:/Users/ramba/Pictures/sec python/RITISH 3.py


The area of the rectangle is 15 square units.
SEC : PROGRAMMING WITH PYTHON

NAME - RITISH KUMAR MAHTO


COURSE - BSC PHYSICS HONOURS
ROLL NO - PHH24095
COLLEGE - MOTILAL NEHRU COLLEGE (UNIVERSITY OF DELHI )

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}

# Getting the size of variables in bytes


print(f"Size of integer: {sys.getsizeof(int_var)} bytes")
print(f"Size of float: {sys.getsizeof(float_var)} bytes")
print(f"Size of string: {sys.getsizeof(str_var)} bytes")
print(f"Size of list: {sys.getsizeof(list_var)} bytes")
print(f"Size of dictionary: {sys.getsizeof(dict_var)} bytes")

OUTPUT:-

============ RESTART: C:/Users/ramba/Pictures/sec python/RITISH 4.py ===========


Size of integer: 28 bytes
Size of float: 24 bytes
Size of string: 55 bytes
Size of list: 104 bytes
Size of dictionary: 184 bytes
SEC : PROGRAMMING WITH PYTHON

NAME - RITISH KUMAR MAHTO


COURSE - BSC PHYSICS HONOURS
ROLL NO - PHH24095
COLLEGE - MOTILAL NEHRU COLLEGE (UNIVERSITY OF DELHI )

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

NAME - RITISH KUMAR MAHTO


COURSE - BSC PHYSICS HONOURS
ROLL NO - PHH24095
COLLEGE - MOTILAL NEHRU COLLEGE (UNIVERSITY OF DELHI )

PROGRAM NO - 6
TO FINDS ROOTS OF QUADRATIC EQUATION

CODE :-
import cmath # For handling complex roots

def find_roots(a, b, c):


# Calculate discriminant
discriminant = b**2 - 4*a*c

# Calculate two roots using the quadratic formula


root1 = (-b + cmath.sqrt(discriminant)) / (2 * a)
root2 = (-b - cmath.sqrt(discriminant)) / (2 * a)

return root1, root2

# Example usage
a=1
b = -3
c=2

root1, root2 = find_roots(a, b, c)

print(f"The roots of the quadratic equation are: {root1} and {root2}")

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

NAME - RITISH KUMAR MAHTO


COURSE - BSC PHYSICS HONOURS
ROLL NO - PHH24095
COLLEGE - MOTILAL NEHRU COLLEGE (UNIVERSITY OF DELHI )

PROGRAM NO - 7
CONVERTING PLANE POLAR TO CARTESIAN COORDINATES
AND VICE VERSA.

CODE :-

import math

def polar_to_cartesian(r, theta):


x = r * math.cos(theta) # Convert r and theta to x-coordinate
y = r * math.sin(theta) # Convert r and theta to y-coordinate
return x, y

# 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

def cartesian_to_polar(x, y):


r = math.sqrt(x**2 + y**2) # Calculate the radial distance
theta = math.atan2(y, x) # Calculate the angle in radians
return r, math.degrees(theta) # Convert theta from radians to degrees

# 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°)

You might also like