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

Complete_Python_Programs_with_Outputs

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)
18 views

Complete_Python_Programs_with_Outputs

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

All Python Programs with Questions and Outputs

1. Check if a Number is Perfect

def is_perfect(num):

divisors = []

for i in range(1, num // 2 + 1):

if num % i == 0:

divisors.append(i)

return sum(divisors) == num

number = 28

if is_perfect(number):

print(f"{number} is a perfect number.")

else:

print(f"{number} is not a perfect number.")

Output:

28 is a perfect number.

2. Find GCD of Two Numbers

def gcd(a, b):

while b != 0:

a, b = b, a % b

return a

num1 = 56

num2 = 98
result = gcd(num1, num2)

print(f"The GCD of {num1} and {num2} is {result}.")

Output:

The GCD of 56 and 98 is 14.

3. Calculate the Sum of an Arithmetic Progression (AP)

def sum_of_ap(a, d, n):

sum_ap = (n / 2) * (2 * a + (n - 1) * d)

return sum_ap

a = 5

d = 2

n = 10

sum_ap = sum_of_ap(a, d, n)

print(f"The sum of the first {n} terms of the AP is {sum_ap}.")

Output:

The sum of the first 10 terms of the AP is 95.0.

4. Transpose of a Matrix

def transpose_matrix(matrix):

rows, cols = len(matrix), len(matrix[0])

transpose = [[0 for _ in range(rows)] for _ in range(cols)]

for i in range(rows):

for j in range(cols):

transpose[j][i] = matrix[i][j]

return transpose
matrix = [

[1, 2, 3],

[4, 5, 6],

[7, 8, 9]

result = transpose_matrix(matrix)

print("The transpose of the matrix is:")

for row in result:

print(row)

Output:

[1, 4, 7]

[2, 5, 8]

[3, 6, 9]

5. Bubble Sort

def bubble_sort(arr):

n = len(arr)

for i in range(n):

for j in range(0, n - i - 1):

if arr[j] > arr[j + 1]:

arr[j], arr[j + 1] = arr[j + 1], arr[j]

return arr

numbers = [64, 34, 25, 12, 22, 11, 90]

sorted_numbers = bubble_sort(numbers)
print(f"Sorted array: {sorted_numbers}")

Output:

Sorted array: [11, 12, 22, 25, 34, 64, 90]

6. Palindrome Number Check

def is_palindrome_number(num):

original_num = num

reversed_num = 0

while num > 0:

digit = num % 10

reversed_num = reversed_num * 10 + digit

num //= 10

return original_num == reversed_num

number = 121

if is_palindrome_number(number):

print(f"{number} is a palindrome number.")

else:

print(f"{number} is not a palindrome number.")

Output:

121 is a palindrome number.

7. Count Frequency of Each Word in a String

def word_frequency(string):

words = string.split()

frequency = {}
for word in words:

word = word.lower()

frequency[word] = frequency.get(word, 0) + 1

return frequency

text = "Python programming is fun. Programming with Python is great."

frequencies = word_frequency(text)

print("Word frequencies:")

for word, count in frequencies.items():

print(f"{word}: {count}")

Output:

python: 2

programming: 2

is: 2

fun.: 1

with: 1

great.: 1

8. Factorial of a Number Using Iteration

def factorial_iterative(n):

if n < 0:

return "Factorial does not exist for negative numbers."

factorial = 1

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

factorial *= i

return factorial
number = 6

result = factorial_iterative(number)

print(f"The factorial of {number} is {result}.")

Output:

The factorial of 6 is 720.

9. Generate Pascal's Triangle

def generate_pascals_triangle(n):

triangle = []

for i in range(n):

row = [1]

if i > 0:

for j in range(1, i):

row.append(triangle[i - 1][j - 1] + triangle[i - 1][j])

row.append(1)

triangle.append(row)

return triangle

rows = 5

triangle = generate_pascals_triangle(rows)

print("Pascal's Triangle:")

for row in triangle:

print(row)

Output:

[1]
[1, 1]

[1, 2, 1]

[1, 3, 3, 1]

[1, 4, 6, 4, 1]

You might also like