0% found this document useful (0 votes)
32 views8 pages

First Term

The document contains a series of Python scripts that perform various mathematical calculations and operations, including area and perimeter calculations for geometric shapes, temperature conversions, variable swapping, finding the smallest and largest numbers, checking for prime numbers, and more. It also includes scripts for calculating factorials, Fibonacci series, GCD and LCM, palindrome checks, and Armstrong and Neon number checks. Additionally, it covers iterations and list manipulations, demonstrating a wide range of programming concepts and techniques.

Uploaded by

Ardnas Attad
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)
32 views8 pages

First Term

The document contains a series of Python scripts that perform various mathematical calculations and operations, including area and perimeter calculations for geometric shapes, temperature conversions, variable swapping, finding the smallest and largest numbers, checking for prime numbers, and more. It also includes scripts for calculating factorials, Fibonacci series, GCD and LCM, palindrome checks, and Armstrong and Neon number checks. Additionally, it covers iterations and list manipulations, demonstrating a wide range of programming concepts and techniques.

Uploaded by

Ardnas Attad
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

L = int(input("Enter the value of a side of a square: "))

Area = L * L
Perimeter = 4 * L
print("The area of the square is:", Area, "\nThe perimeter of the square is:", Perimeter)

L = float(input("Enter the length of the cuboid: "))


B = float(input("Enter the breadth of the cuboid: "))
H = float(input("Enter the height of the cuboid: "))
TSA = 2 * (L * B + B * H + H * L)
LSA = 2 * (L + B) * H
Vol = L * B * H
print("The total surface area of the cuboid is:", TSA)
print("The lateral surface area of the cuboid is:", LSA)
print("The volume of the cuboid is:", Vol)

F = float(input("Enter temperature in fahrenheit(°F): "))


C = (5 / 9) * (F - 32)
print("The given temperature in celsius is: " + str(C) + "°C")

A = float(input("Enter first variable: "))


B = float(input("Enter second variable: "))
A += B
B=A-B
A -= B
print("Value of first variable after swapping is: ", A)
print("Value of second variable after swapping is: ", B)

A = float(input("Enter first variable: "))


B = float(input("Enter second variable: "))
C=A
A=B
B=C
print("Value of first variable after swapping is: ", A)
print("Value of second variable after swapping is: ", B)

A = float(input("Enter first number: "))


B = float(input("Enter second number: "))
C = float(input("Enter third number: "))
D = float(input("Enter fourth number: "))
if (A < B) and (A < C) and (A < D):
smallest = A
elif (B < A) and (B < C) and (B < D):
smallest = B
elif (C < A) and (C < B) and (C < D):
smallest = C
else:
smallest = D
print("The smallest number is: ", smallest)

year = int(input("Enter a year: "))


if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")

import cmath
import math

a = float(input("Enter the coefficient of x^2 (a): "))


b = float(input("Enter the coefficient of x (b): "))
c = float(input("Enter the constant (c): "))
d = b ** 2 - 4 * a * c
if d > 0:
r1 = (-b + math.sqrt(d)) / (2 * a)
r2 = (-b - math.sqrt(d)) / (2 * a)
print("The roots are", r1, "and", r2, "and they are real and distinct")
elif d == 0:
r1 = r2 = -b / (2 * a)
print("The roots are real and same and the root is", r1)
else:
r1 = (-b - cmath.sqrt(d)) / (2 * a)
r2 = (-b + cmath.sqrt(d)) / (2 * a)
print("The roots are", r1, "and", r2, " and are complex and distinct")
import math

ch = int(input(
"Enter 1 to print the area and circumference of a circle\nEnter 2 to print the area and perimeter
of a rectangle\nEnter your choice: "))
if ch == 1:
r = float(input("Enter the radius of the circle: "))
print("The area of the circle is: ", math.pi * (math.pow(r, 2)))
print("The circumference of the circle is: ", (2 * math.pi * r))
elif ch == 2:
l = float(input("Enter the length of the rectangle: "))
w = float(input("Enter the width of the rectangle: "))
print("The area of the rectangle is: ", (l * w))
print("The perimeter of the rectangle is: ", 2 * (l + w))
else:
print("Wrong choice! Try again!")

n = int(input("Enter the value of 'N' to print the sum of the first 'N' natural numbers: "))
s=0
for x in range(1, n + 1):
s += x
print("The sum of the first '", n, "' natural numbers is: ", s)

n = int(input("Enter the number: "))


s=1
for x in range(1, (n + 1), 1):
s *= x
print("The factorial of '" + str(n) + "' is: ", s)

n = int(input("Enter a number: "))


c=0
for x in range(1, n, 1):
if (n % x) == 0:
c += 1
if c == 1:
print(n, "is a prime number")
else:
print(n, "isn't a prime number")

n = int(input("Enter the length of fibonacci series: "))


n1, n2, c = 0, 1, 0
print("The Fibonacci series is:")
while c < n:
print(n1, end=" ")
n3 = n1 + n2
n1 = n2
n2 = n3
c += 1

n1 = x = int(input("Enter the first number: "))


n2 = y = int(input("Enter the second number: "))
while n2 != 0:
t = n2
n2 = n1 % n2
n1 = t
gcd = n1
lcm = (x * y) / gcd
print("GCD of", x, "and", y, "is:", gcd)
print("LCM of", x, "and", y, "is:", lcm)

n = cp = int(input("Enter a number: "))


s=0
while n != 0:
s = s * 10 + n % 10
n //= 10
if s == cp:
print(cp, "is a palindrome number")
else:
print(cp, "isn't a palindrome number")

for i in range(0, 5):


for j in range(0, 5):
print("*", end=" ")
print(" ")
# Procedure – 1 Write a python script to interchange the value of two variables – (a) using 3rd
variable (b)without using 3rd variable (c) using special code.
ch = int(input(
"To interchange the value of 2 variables:\nEnter '1' for using a 3rd variable\nEnter '2' for not
using 3rd variable\nEnter '3' for using special code\nEnter your choice:"))
if ch == 1:
A = float(input("Enter first variable: "))
B = float(input("Enter second variable: "))
C=A
A=B
B=C
print("Value of first variable after swapping is: ", A)
print("Value of second variable after swapping is: ", B)
elif ch == 2:
A = float(input("Enter first variable: "))
B = float(input("Enter second variable: "))
A += B
B=A-B
A -= B
print("Value of first variable after swapping is: ", A)
print("Value of second variable after swapping is: ", B)
elif ch == 3:
A = float(input("Enter first variable: "))
B = float(input("Enter second variable: "))
A, B = B, A
print("Value of first variable after swapping is: ", A)
print("Value of second variable after swapping is: ", B)
else:
print("Wrong choice! Try Again!")

# Procedure – 2 Write a python script to input temperature as per user choice: 1- Celsius Scale to
Fahrenheit Scale 2- Fahrenheit Scale to Celsius Scale.
ch = int(input(
"Enter '1' for conversion from Celsius to Fahrenheit scale\nEnter '2' for conversion from
Fahrenheit to Celsius scale\nEnter your choice:"))
if ch == 1:
C = float(input("Enter temperature in Celsius(°C): "))
F = (C * (9 / 5)) + 32
print("The given temperature in Fahrenheit is: " + str(F) + "°F")
elif ch == 2:
F = float(input("Enter temperature in fahrenheit(°F): "))
C = (5 / 9) * (F - 32)
print("The given temperature in celsius is: " + str(C) + "°C")
else:
print("Wrong choice! Try Again!")

# Selection – 1 Write a python script to input 4 numbers and display the largest of them.
A = float(input("Enter first number: "))
B = float(input("Enter second number: "))
C = float(input("Enter third number: "))
D = float(input("Enter fourth number: "))
if (A > B) and (A > C) and (A > D):
largest = A
elif (B > A) and (B > C) and (B > D):
largest = B
elif (C > A) and (C > B) and (C > D):
largest = C
else:
largest = D
print("The smallest number is: ", largest)

# Selection – 2 Write a python script to check whether a year is Leap Year or not.
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")

# Selection – 3 Write a python script to solve the quadratic equation ax2 + bx + c = 0 where a ≠
0, stating the nature of roots.

a = float(input("Enter the coefficient of x^2 (a): "))


b = float(input("Enter the coefficient of x (b): "))
c = float(input("Enter the constant (c): "))
d = b ** 2 - 4 * a * c
if d > 0:
r1 = (-b + (d ** (1 / 2))) / (2 * a)
r2 = (-b - (d ** (1 / 2))) / (2 * a)
print("The roots are", r1, "and", r2, "and they are real and distinct")
elif d == 0:
r1 = r2 = -b / (2 * a)
print("The roots are real and same and the root is", r1)
else:
print("The roots are complex and distinct")

# Write a python script to calculate and display the result of n∑(k**2) , k=1 where N is accepted
as user input.
n = int(input("Enter the value of 'N' to print the sum: "))
s=0
for k in range(1, n + 1):
s += k ** 2
print("The sum is: ", s)

# Iteration – 2 Write a python script to input a number and print its factorial value.
n = int(input("Enter the number: "))
s=1
for x in range(1, (n + 1)):
s *= x
print("The factorial of '" + str(n) + "' is: ", s)

# Iteration – 3 Write a python script to read a number and check it is prime or not.
n = int(input("Enter a number: "))
c=0
for x in range(1, n):
if (n % x) == 0:
c += 1
if c == 1:
print(n, "is a prime number")
else:
print(n, "isn't a prime number")

# Iteration – 4 Write a python script to read a number and calculate the sum of its digits.
n = int(input("Enter a variable: "))
s=0
while n != 0:
s += int(n % 10)
n /= 10
print("Sum of the digits is: ", s)

# Iteration – 5 Write a python script to read a number and check :


# a. Armstrong Number
# b. Neon Number
ch = int(input(
"Enter '1' for checking for an Armstrong number\nEnter '2' for checking for a Neon
Number\nEnter your choice:"))
if ch == 1:
a = int(input("Enter a number: "))
s=0
c=a
l = len(str(a))
while c > 0:
s += (c % 10) ** l
c //= 10
if a == s:
print(a, "is an Armstrong number")
else:
print(a, "is not an Armstrong number")
elif ch == 2:
n = int(input("Enter a number: "))
c=n*n
s=0
while c != 0:
s += int(c % 10)
c /= 10
if s == n:
print(n, " is a neon number")
else:
print(n, " isn't a neon number")
else:
print("Wrong choice! Try Again!")

# Iteration – 6 Write a python script(s) to calculate the sum of the following series :
# i. S = 1 + x + x^2 + x^3 + ….. + x^n
# ii. S = 1 + x – x^2/2!+ x^3/3! – ….. up to n terms
# iii. S = 1 + (1+2) + (1+2+3) + ….. + (1+2+3+ ….. + n)
# iv. S= 0 + 1 + 1 + 2 + 3 + ….. up to n terms.
ch = int(input("Enter your choice: "))
n = int(input("Enter the value of 'n': "))
x = float(input("Enter the value of 'x': "))
s=0
if ch == 1:
for i in range(1, n + 1):
s += x ** i
print("The sum is: ", s + 1)
elif ch == 2:
for i in range(1, n + 1):
f=1
for j in range(1, (i + 1)):
f *= j
if i % 2 == 0:
s -= (x ** i) / f
elif i % 2 == 1:
s += (x ** i) / f
print("The sum is: ", s + 1)
elif ch == 3:
for i in range(1, n + 1):
s += i * (i + 1) / 2
print("The sum is: ", s)
elif ch == 4:
n1, n2, c, s = 0, 1, 0, 0
while c < n:
s += n1
n3 = n1 + n2
n1 = n2
n2 = n3
c += 1
print("The sum is: ", s)
else:
print("Wrong choice! Try Again!")

# Iteration – 7 Write a python script to print the following pattern :


#1
#23
#456
# 7 8 9 10
# 11 12 13 14 15
c=1
for i in range(0, 5):
for j in range(0, i + 1):
print(c, end=" ")
c += 1
print()

# List – 1 Write a python script to create two list of integers L1[ ] and L2[ ] respectively , then
join L2[ ] in reverse order after L1[ ].
L1 = eval(input("Enter the elements of L1 in [ ] separated by ',' : "))
L2 = eval(input("Enter the elements of L2 in [ ] separated by ',' : "))
L2.reverse()
L1.extend(L2)
print(L1)
# List – 2 Write a python script to create a list of integers and count the number of integers
ending with 5.
L1 = eval(input("Enter the elements in [ ] separated by ',' : "))
c=0
for i in L1:
if i % 10 == 5:
c=c+1
print("The number of integers ending in '5' are: ", c)

# List – 3 Write a python script to implement linear search in a user inputted list of integers.
L1 = eval(input("Enter the elements in [ ] separated by ',' : "))
x = int(input("Enter the element to be searched for: "))
for i in range(len(L1)):
if L1[i] == x:
print("The element '", x, "' is present in ", L1, " at the position of: ", i + 1)

# List – 4 Write a python script to implement bubble sort in a user inputted list of integers.
L1 = eval(input("Enter the elements in [ ] separated by ',' : "))
for i in range(len(L1)):
for j in range(len(L1) - 1):
if L1[j] > L1[j + 1]:
t = L1[j]
L1[j] = L1[j + 1]
L1[j + 1] = t
print("The sorted list is: ", L1)

You might also like