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

Python Programs 1

The document contains a collection of Python programs that demonstrate various programming concepts including finding the largest of three integers, checking leap years, calculating areas and perimeters of circles, and more. Each program is accompanied by a brief explanation of its functionality and includes code snippets. The topics covered range from basic arithmetic operations to more complex algorithms like checking for prime numbers and calculating factorials.

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)
4 views7 pages

Python Programs 1

The document contains a collection of Python programs that demonstrate various programming concepts including finding the largest of three integers, checking leap years, calculating areas and perimeters of circles, and more. Each program is accompanied by a brief explanation of its functionality and includes code snippets. The topics covered range from basic arithmetic operations to more complex algorithms like checking for prime numbers and calculating factorials.

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:
print('This year is a leap year')
else:
print('This year is not a leap year')
20. Write a program to obtain x, y, z and calculate 4x⁴+3y³+9z+6π.
# Write a program to display a menu for calculating
# area of circle or perimeter of the circle.
r=float(input('Enter the radius of the circle:'))
print('[Link] perimeter')
print('[Link] area')
choice=int(input('Enter your choice (1 or 2):'))
if choice==1:
peri=2*3.14159*r
print('Perimeter of the circle with radius',r,':',peri)
else:
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.
# Print the sum progressively i.e. after adding each natural number,
# print sum so far.
Sum=0
for n in range(1,21):
Sum+=n
print('Sum of natural numbers <=',n,'is',Sum)

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

# 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>1000 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


The factorial of n is the product of all positive integers from 1 to n. For example, factorial(5) = 5
× 4 × 3 × 2 × 1 = 120. This program uses recursion, a function that calls itself to compute it.
Code:
# 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

7. Check Prime Number


A prime number is greater than 1 and has no divisors other than 1 and itself. This program uses
an efficient algorithm that only tests divisors up to the square root of the number.
Code:
# defining the prime check function
def prime_chk(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

ans = int(input("Enter a number to check for prime: "))


print(f"The number {ans} is prime: {prime_chk(ans)}")

9. Find Sum of Digits


This program calculates the sum of all individual digits in a number. For example, the digits of
251 add up to 2 + 5 + 1 = 8. This is a common Python program for beginners that practises loops
and modulus arithmetic.
Code:
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}")


Output:
Enter any number: 251
The sum of digits: 8
How it works: Modulus 10 extracts the last digit of num each iteration and adds it to the
running sum. Integer division by 10 removes that digit. For 251: iteration 1 adds 1, iteration 2
adds 5, iteration 3 adds 2, giving a total of 8.
10. 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.
Code:
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

30. Check if a String is a Palindrome


A palindrome is a string that reads the same forwards and backwards “level”, “racecar”,
“madam”. Palindrome check is one of the most commonly asked Python string programs in
interviews.
Code:
# defining the palindrome function
def palindrome_chk(string):
# Compare the string with its reverse
return string == string[::-1]

# Example usage
my_string = "level"
if palindrome_chk(my_string):
print(f"The string {my_string} is a palindrome.")
else:
print(f"The string {my_string} is not a palindrome.")

45. How to sum the digits of the number the user entered on Python?
number = input("enter a number: ")
sum=0
for numberofdigit in number:
sum += int(numberofdigit)

print("the sum of the digits of the number:",sum)

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 = " ")

29. How to check if there is a specified character in a string on Python?


char_list = ["a", "b" ,"c"]
string = "abcd"
matched_list = [characters in char_list for characters in string]
print(matched_list)
OUTPUT
[True, True, True, False]
string_contains_chars = all(matched_list)
print(string_contains_chars)

24. How to calculate the area and circumference of the circle whose radius is entered
using the function on Python?
import math

def find_Diameter(radius):
return 2 * radius

def find_Circumference(radius):
return 2 * [Link] * radius

def find_Area(radius):
return [Link] * radius * radius

r = float(input(' Please Enter the radius of a circle: '))

diameter = find_Diameter(r)
circumference = find_Circumference(r)
area = find_Area(r)

print("\n Diameter Of a Circle = %.2f" %diameter)


print(" Circumference Of a Circle = %.2f" %circumference)
print(" Area Of a Circle = %.2f" %area)

25. How to calculate the area of the rectangle, whose width and height are entered using
the function on Python?
def areaRectangle(a, b):
return (a * b)

def perimeterRectangle(a, b):


return (2 * (a + b))
a = 5;
b = 6; print ("Area = ", areaRectangle(a, b))

print ("Perimeter = ", perimeterRectangle(a, b))

21. How to find out if the entered number is Prime or Not on Python?
num = int(input("Enter a number: "))

if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")

else:
print(num,"is not a prime number")
19. How to show the sum of numbers between two numbers the user has entered on
Python?
sumofnumbers=0;
num1 = input('first number: ')
num2 = input('second number: ')
for i in range(int(sayi1)+1,int(sayi2)):
sumofnumbers+=i
print("Sum of numbers between {0} and {1} : {2}".format(num1,num2,sumofnumbers))
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)

8. How to find out if the entered number is odd or even on Python?

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


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

10. How to find out if the entered number is Positive, Negative, or 0 on Python?
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

11. Read a text file 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