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

Python Programs

The document contains various Python programs demonstrating basic programming concepts such as swapping numbers, checking even or odd, generating multiplication tables, type casting, voting eligibility, and more. Each program includes user input and outputs results based on the logic implemented. Additionally, it covers finding maximum values in a list, summing digits of a number, checking for prime numbers, creating star patterns, and determining the greatest of three numbers.

Uploaded by

abestgamer57
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 views3 pages

Python Programs

The document contains various Python programs demonstrating basic programming concepts such as swapping numbers, checking even or odd, generating multiplication tables, type casting, voting eligibility, and more. Each program includes user input and outputs results based on the logic implemented. Additionally, it covers finding maximum values in a list, summing digits of a number, checking for prime numbers, creating star patterns, and determining the greatest of three numbers.

Uploaded by

abestgamer57
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 swap two numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Before Swap:", a, b)
a, b = b, a
print("After Swap:", a, b)

2. Program to check even or odd


num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is Even")
else:
print(num, "is Odd")

3. Program for multiplication table


n = int(input("Enter a number: "))
for i in range(1, 11):
print(n, "x", i, "=", n*i)

4. Type Casting: String to Integer and Float


num_str = input("Enter a number: ")
num_int = int(num_str)
num_float = float(num_str)
print("Integer:", num_int)
print("Float:", num_float)

5. Program to check voting eligibility (Conditional)


age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")
6. Program to print numbers 1 to n using while loop
n = int(input("Enter a number: "))
i=1
while i <= n:
print(i, end=" ")
i += 1

7. Program to find sum of odd numbers up to n


n = int(input("Enter a number: "))
sum_odd = 0
for i in range(1, n+1):
if i % 2 != 0:
sum_odd += i
print("Sum of odd numbers =", sum_odd)

8. Program to find maximum in a list


lst = [23, 45, 12, 67, 89, 34]
max_num = lst[0]
for num in lst:
if num > max_num:
max_num = num
print("Largest number =", max_num)

9. Program to find sum of digits of a 3-digit number


num = int(input("Enter a 3-digit number: "))
sum_digits = 0
temp = num
while temp > 0:
sum_digits += temp % 10
temp //= 10
print("Sum of digits =", sum_digits)
Sample Output:
Enter a 3-digit number: 456
Sum of digits = 15

10. Program to check Prime Number


n = int(input("Enter a number: "))
if n > 1:
for i in range(2, int(n**0.5)+1):
if n % i == 0:
print(n, "is not prime")
break
else:
print(n, "is prime")
else:
print(n, "is not prime")
11. Star Pattern (Right Triangle)
rows = int(input("Enter rows: "))
for i in range(1, rows+1):
print("*" * i)

12. Greatest of Three Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b and a > c:
print(a, "is greatest")
elif b > c:
print(b, "is greatest")
else:
print(c, "is greatest")

You might also like