KEMBAR78
List of Python Program | PDF | Control Flow | Algorithms
0% found this document useful (0 votes)
1 views6 pages

List of Python Program

Uploaded by

ehtsalman
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)
1 views6 pages

List of Python Program

Uploaded by

ehtsalman
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/ 6

LIST OF PYTHON PROGRAM

1. # Program to print Fibonacci series


Using For Loop
# Fibonacci using for loop

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

a, b = 0, 1
print("Fibonacci Sequence:")

for i in range(n):
print(a, end=" ")
a, b = b, a + b

Using While Loop

# Fibonacci using while loop

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

a, b = 0, 1
count = 0

print("Fibonacci Sequence:")
while count < n:
print(a, end=" ")
a, b = b, a + b
count += 1

2. # Program to check whether a number is Prime or Composite

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

if num <= 1:
print(num, "is neither Prime nor Composite.")
else:
is_prime = True
for i in range(2, int(num**0.5) + 1): # check up to sqrt(num)
if num % i == 0:
is_prime = False
break

if is_prime:
print(num, "is a Prime number.")
else:
print(num, "is a Composite number.")

3. Right-Angled Triangle Pattern


rows = 5
for i in range(1, rows+1):
print("*" * i)

Output:

*
**
***
****
*****

4. Inverted Triangle Pattern


rows = 5
for i in range(rows, 0, -1):
print("*" * i)

Output:

*****
****
***
**
*

4. Number Triangle
rows = 5
for i in range(1, rows+1):
for j in range(1, i+1):
print(j, end=" ")
print()

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

5. Floyd’s Triangle
rows = 5
num = 1
for i in range(1, rows+1):
for j in range(1, i+1):
print(num, end=" ")
num += 1
print()

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 1

6. Pascal’s Triangle
rows = 5
for i in range(rows):
print(" "*(rows-i), end="")
num = 1
for j in range(i+1):
print(num, end=" ")
num = num * (i - j) // (j + 1)
print()

Output:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

7. Python Program: Income Tax Calculator (India – New Regime 2024-25)


# Income Tax Calculator (New Regime FY 2024-25)

income = float(input("Enter your annual income (₹): "))

tax = 0

if income <= 300000:


tax = 0
elif income <= 700000:
tax = (income - 300000) * 0.05
elif income <= 1000000:
tax = (400000 * 0.05) + (income - 700000) * 0.10
elif income <= 1200000:
tax = (400000 * 0.05) + (300000 * 0.10) + (income - 1000000) * 0.15
elif income <= 1500000:
tax = (400000 * 0.05) + (300000 * 0.10) + (200000 * 0.15) + (income - 1200000) * 0.20
else:
tax = (400000 * 0.05) + (300000 * 0.10) + (200000 * 0.15) + (300000 * 0.20) + (income -
1500000) * 0.30

# Tax rebate under section 87A (if income ≤ 7 lakh → No tax)


if income <= 700000:
tax = 0

print("Total Tax Payable: ₹", tax)

8. # Program to find HCF of two numbers without built-in functions

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

b = int(input("Enter second number: "))

# Find smaller number

if a < b:

smaller = a

else:

smaller = b

hcf = 1 # initialize

for i in range(1, smaller+1):

if (a % i == 0) and (b % i == 0):

hcf = i

print("HCF of", a, "and", b, "is:", hcf)

9. # Program to find LCM without using built-in functions

num1 = int(input("Enter first number: "))


num2 = int(input("Enter second number: "))

# Find greater number to start with

if num1 > num2:

greater = num1

else:

greater = num2

while True:

if (greater % num1 == 0) and (greater % num2 == 0):

lcm = greater

break

greater += 1

print("LCM of", num1, "and", num2, "is:", lcm)

10. Factorial program


# Factorial using loop

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


fact = 1

if num < 0:
print("Factorial does not exist for negative numbers.")
elif num == 0:
print("Factorial of 0 is 1.")
else:
for i in range(1, num+1):
fact *= i
print("Factorial of", num, "is", fact)

11. Fibonacci program


# Fibonacci using for loop

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

a, b = 0, 1
print("Fibonacci Sequence:")

for i in range(n):
print(a, end=" ")
a, b = b, a + b

You might also like