0% found this document useful (0 votes)
12 views4 pages

Python Programs for Basic Algorithms

The document contains a series of Python programs that demonstrate various programming concepts such as finding the largest and smallest numbers in a list, calculating sums, checking for even or odd numbers, and implementing sorting algorithms. It also includes functions for calculating factorials, checking for palindromes, and performing linear and binary searches. Each program is presented with its code and a brief description of its functionality.
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)
12 views4 pages

Python Programs for Basic Algorithms

The document contains a series of Python programs that demonstrate various programming concepts such as finding the largest and smallest numbers in a list, calculating sums, checking for even or odd numbers, and implementing sorting algorithms. It also includes functions for calculating factorials, checking for palindromes, and performing linear and binary searches. Each program is presented with its code and a brief description of its functionality.
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

1.

Program to print the largest and smallest number from a list


numbers = [10, 3, 25, 7, 18]
largest = max(numbers)
smallest = min(numbers)
print("Largest:", largest)
print("Smallest:", smallest)

2. Program to find sum of all numbers in a list


numbers = [10, 20, 30, 40]
total = sum(numbers)
print("Sum:", total)

3. Program to print even and odd numbers in a range


start = 1
end = 20
for i in range(start, end + 1):
if i % 2 == 0:
print(i, "is Even")
else:
print(i, "is Odd")

4. Program to calculate factorial


n = 5
fact = 1
for i in range(1, n + 1):
fact *= i
print("Factorial:", fact)

5. Program to print multiplication table


n = 7
for i in range(1, 11):
print(n, "x", i, "=", n * i)

6. Program to find average of 3 numbers


a, b, c = 5, 10, 15
avg = (a + b + c) / 3
print("Average:", avg)

7. Program to check positive, negative or zero


n = -5
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")

8. Program to count numbers divisible by 5 (1 to 50)


count = 0
for i in range(1, 51):
if i % 5 == 0:
count += 1
print("Count:", count)
9. Program to print first 10 Fibonacci numbers
a, b = 0, 1
print(a, b, end=" ")
for i in range(3, 11):
c = a + b
print(c, end=" ")
a, b = b, c

10. Convert Celsius to Fahrenheit


C = 25
F = (C * 9/5) + 32
print("Fahrenheit:", F)

11. Reverse digits of a number


n = 12345
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n //= 10
print("Reversed:", rev)

12. Check palindrome number


n = 121
temp = n
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n //= 10
if rev == temp:
print("Palindrome")
else:
print("Not Palindrome")

13. Largest of three numbers


a, b, c = 15, 22, 9
print("Largest:", max(a, b, c))

14. Sum of even numbers in a list


numbers = [1, 2, 3, 4, 5, 6]
total = 0
for n in numbers:
if n % 2 == 0:
total += n
print("Sum of evens:", total)

15. Count odd numbers in a list


numbers = [1, 2, 3, 4, 5, 6]
count = 0
for n in numbers:
if n % 2 != 0:
count += 1
print("Odd count:", count)
16. Bubble Sort (Ascending)
numbers = [5, 2, 9, 1, 5]
for i in range(len(numbers)):
for j in range(0, len(numbers) - i - 1):
if numbers[j] > numbers[j + 1]:
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
print(numbers)

17. Bubble Sort (Descending)


numbers = [5, 2, 9, 1, 5]
for i in range(len(numbers)):
for j in range(0, len(numbers) - i - 1):
if numbers[j] < numbers[j + 1]:
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
print(numbers)

18. Insertion Sort


numbers = [5, 2, 9, 1, 5]
for i in range(1, len(numbers)):
key = numbers[i]
j = i - 1
while j >= 0 and numbers[j] > key:
numbers[j + 1] = numbers[j]
j -= 1
numbers[j + 1] = key
print(numbers)

19. Linear Search


numbers = [5, 2, 9, 1, 5]
target = 9
found = False
for i in range(len(numbers)):
if numbers[i] == target:
found = True
break
if found:
print("Found")
else:
print("Not Found")

20. Binary Search


numbers = [1, 2, 5, 9, 15]
target = 9
low = 0
high = len(numbers) - 1
found = False
while low <= high:
mid = (low + high) // 2
if numbers[mid] == target:
found = True
break
elif target < numbers[mid]:
high = mid - 1
else:
low = mid + 1
if found:
print("Found")
else:
print("Not Found")
21. Sort names alphabetically
names = ["Hassan", "Awais", "Bilal", "Ahmed"]
[Link]()
print(names)

You might also like