0% found this document useful (0 votes)
4 views7 pages

Python Programs

The document contains a series of Python programming tasks and their corresponding solutions. Each task includes a brief description, the program code, and sample output. The tasks cover various topics such as summing numbers, generating multiplication tables, calculating factorials, checking for prime numbers, reversing numbers, and more.
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)
4 views7 pages

Python Programs

The document contains a series of Python programming tasks and their corresponding solutions. Each task includes a brief description, the program code, and sample output. The tasks cover various topics such as summing numbers, generating multiplication tables, calculating factorials, checking for prime numbers, reversing numbers, and more.
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

Question: Write a program to find the sum of numbers from 1 to 10.

Program:

sum = 0
for i in range(1, 11):
sum = sum + i
print("Sum =", sum)

Output:

Sum = 55

Question: Write a program to print the multiplication table of a number.

Program:

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

for i in range(1, 11):


print(num, "x", i, "=", num*i)

Output:

Enter a number: 5
5x1=5
5 x 2 = 10
...
5 x 10 = 50

Question: Write a program to find factorial of a number.

Program:

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


fact = 1

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


fact = fact * i

print("Factorial =", fact)

Output:

Enter a number: 5
Factorial = 120
Question: Write a program to check if a number is prime.

Program:

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


flag = False

for i in range(2, num):


if num % i == 0:
flag = True
break
if flag:
print("Not Prime")
else:
print("Prime Number")

Output:

Enter a number: 7
Prime Number

Question: Write a program to reverse a number.

Program:

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


rev = 0

while num > 0:


digit = num % 10
rev = rev * 10 + digit
num = num // 10

print("Reverse =", rev)

Output:

Enter number: 123


Reverse = 321

Count Digits in a Number

Program:

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


count = 0

while num > 0:


num = num // 10
count += 1

print("Digits =", count)

Output :- Enter number: 4567


Digits = 4
Question: Write a program to check if a number is palindrome.

Program:

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


temp = num
rev = 0

while num > 0:


digit = num % 10
rev = rev * 10 + digit
num = num // 10

if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")

Output:

Enter number: 121


Palindrome

Find Largest in a List

Program

numbers = [10, 25, 5, 40, 15]

print("Largest number:", max(numbers))

Output

Largest number: 40

Find Sum of List Elements

Program

numbers = [2,4,6,8]

print("Sum =", sum(numbers))

Output

Sum = 20
Print List Elements

Program

fruits = ["Apple","Banana","Mango"]

for fruit in fruits:


print(fruit)

Output

Apple
Banana
Mango

Create a Dictionary

Program

student = {"Name":"Rahul","Age":15,"Class":10}

print(student)

Output

{'Name': 'Rahul', 'Age': 15, 'Class': 10}

Access Dictionary Value

Program

student = {"Name":"Rahul","Age":15}

print(student["Name"])

Output

Rahul

Count Vowels in a String

Program

text = input("Enter a string: ")


count = 0

for i in text:
if i in "aeiouAEIOU":
count += 1

print("Vowels:", count)

Output

Enter a string: hello


Vowels: 2
Fibonacci Series

Program

n1 = 0
n2 = 1

for i in range(10):
print(n1)
n1,n2 = n2,n1+n2

Output

0
1
1
2
3
5
8
13
21
34

Convert Celsius to Fahrenheit

Program

c = float(input("Enter temperature in Celsius: "))

f = (c*9/5)+32

print("Fahrenheit:", f)

Output

Enter temperature in Celsius: 25


Fahrenheit: 77.0
Question: Write a Python program to print the following pattern.

1
12
123
1234
12345

Program:

for i in range(1, 6):


for j in range(1, i+1):
print(j, end=" ")
print()

Output:

1
12
123
1234
12345

Question: Write a Python program to print the following pattern.

54321
5432
543
54
5

Program:

for i in range(5, 0, -1):


for j in range(5, 5-i, -1):
print(j, end=" ")
print()

Output:

54321
5432
543
54
5
Question: Write a Python program to print the following pattern.

1
23
456
7 8 9 10
11 12 13 14 15

Program:

num = 1

for i in range(1, 6):


for j in range(1, i+1):
print(num, end=" ")
num += 1
print()

Output:

1
23
456
7 8 9 10
11 12 13 14 15

You might also like