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

python code

Uploaded by

iamsam100806
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)
2 views

python code

Uploaded by

iamsam100806
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/ 3

1.

WAP to find the roots of a quadratic equation:

import cmath

def quadratic_roots(a, b, c):

discriminant = (b ** 2) - (4 * a * c)

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

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

return root1, root2

a = int(input("coefficient of x**2:"))

b = int(input("coeficient of x:"))

c = int(input("constant term:"))

print(quadratic_roots(a, b, c))

2. Prime number operations:

a. Check if a number is prime:

def is_prime(n):

if n < 2:

return("n is not a prime number")

for i in range(2, n):

if n % i == 0:

return("n is not a prime number")

return("n is a prime number")

n = int(input("write any integer:"))

print(is_prime(n))

b. Generate all prime numbers till n:

def is_prime(n):

if n < 2:

return("not a prime number")


for i in range(2, n):

if n % i == 0:

return("n is not a prime number")

return("is a prime number")

n = int(input("write any integer:"))

print(is_prime(n))

def primes_till_n(n):

for i in range(2, n+1):

if is_prime(i) == "is a prime number":

print(i)

print(primes_till_n(n))

c. Generate the first n prime numbers:

def is_prime(n):

if n < 2:

return("not a prime number")

for i in range(2, n):

if n % i == 0:

return("n is not a prime number")

return("is a prime number")

n = int(input("write any integer:"))

print(is_prime(n))

def primes_till_n(n):

for i in range(2, n+1):

if is_prime(i) == "is a prime number":

print(i)

print(primes_till_n(n))

def first_n_primes(n):

primes = []
num = 2

while len(primes) < n:

if is_prime(num)== "is a prime number":

primes.append(num)

num += 1

return primes

print(first_n_primes(n))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy