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

Python Programs

The document contains various Python programs that perform tasks such as finding the largest among three integers, checking for leap years, calculating the area and perimeter of a circle, summing natural numbers, calculating factorials, reversing numbers, checking Armstrong numbers, and listing even and odd numbers. It also includes a program to count vowels in a given string. Each program is presented with input prompts and corresponding output statements.

Uploaded by

sivangsantonino
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)
3 views3 pages

Python Programs

The document contains various Python programs that perform tasks such as finding the largest among three integers, checking for leap years, calculating the area and perimeter of a circle, summing natural numbers, calculating factorials, reversing numbers, checking Armstrong numbers, and listing even and odd numbers. It also includes a program to count vowels in a given string. Each program is presented with input prompts and corresponding output statements.

Uploaded by

sivangsantonino
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

# Write a program to find largest among three integers

a=int(input('Enter the first integer:'))


b=int(input('Enter the second integer:'))
c=int(input('Enter the third integer:'))
if a>b and a>c:
print(a, 'is the largest integer')
if b>a and b>c:
print(b, 'is the largest integer')
if c>a and c>b:
print(c, 'is the largest integer')
# Write a program to accept the year and
# check if it is a leap year or not.
a=int(input('Enter the year:'))
if (a%4==0 and a%100!=0) or a%400==0:
print('This year is a leap year')
else:
print('This year is not a leap year')

# area of circle or perimeter of the circle.


r=float(input('Enter the radius of the circle:'))
peri=2*3.14159*r
print('Perimeter of the circle with radius',r,':',peri)
area=3.14159*r*r
print('Area of the circle of the radius',r,':',area)

# Write a program to print the sum of natural numbers between 1 to 20.

Sum=0
for n in range(1,21):
Sum+=n
print('Sum of natural numbers <=',n,'is',Sum)

# Write a program to calculate the factorial of a number.


num=int(input('Enter a number:'))
fact=1
a=1
while a<=num:
fact*=a
a+=1
print('The factorial of',num,'is',fact)

# Write a program to create a triangle of stars using nested loop.


for i in range(1,6):
print()
for j in range(1,i):
print('*',end=' ')
# Write a program to read an integer and reverse the number.
num=int(input('Enter a number (>1000):'))
tnum=num
reverse=0
while tnum>0:
digit=tnum%10
reverse=reverse*10+digit
tnum=tnum//10
print('Reverse of',num,'is',reverse)

5. Find Factorial of a Number,This program uses recursion, a function that calls itself to
compute it.

# define a factorial function


def factorial(num):
if num == 0: # return 1 if num is 0
return 1
return num * factorial(num - 1) # return factorial of num

ans = int(input("Enter a number to find factorial: ")) # getting input to find factorial
print(f"The Factorial of {ans} is {factorial(ans)}") # printing factorial of a number

9. Find Sum of Digits

num = int(input("Enter any number: ")) # taking an input number


sum = 0

# while loop
while num != 0:
sum += num % 10 # adding sum with the remainder of input number
num //= 10

print(f"The sum of digits: {sum}")

# Check Armstrong Number


An Armstrong number (also called a narcissistic number) is a number equal to the sum of its
digits each raised to the power of the number of digits. For example, 153 = 1³ + 5³ + 3³ = 153.

n = int(input("Enter a number: ")) # taking an input number


sum_cubes = sum(int(digit) ** 3 for digit in str(n)) # finding sum cubes

if n == sum_cubes:
print(f"The number {n} is an Armstrong number") # printing if n is armstrong
else:
print(f"The number {n} is not an Armstrong number") # printing if n is not Armstrong

42. How to print even numbers in a list on Python?


list1 = [10, 21, 4, 45, 66, 93]

for num in list1:

# checking condition
if num % 2 == 0:
print(num, end = " ")

13. How to List Even Numbers 1–100 on Python?


for i in range(1,101):
if i%2==0:
print(i)
14. How to List Odd Numbers from 1–100 on Python?
for i in range(1,101):
if i%2!=0:
print(i)

# Write a program to find whether a given number is even or odd.


a=int(input('Enter the number:'))
if a%2==0:
print('The number is even')
else:
print('The number is odd')

11. Read a text and display the number of vowels

line =input(“enter string”)


count_vow = 0
count_low = 0
print(line)
for ch in line:
if ch in 'aeiouAEIOU':
count_vow += 1
print("Vowels: ",count_vow)

You might also like