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

Python Programs1

Download as docx, pdf, or txt
0% found this document useful (0 votes)
17 views7 pages

Python Programs1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

# function to check string is

# palindrome or not

def isPalindrome(str):

# Run loop from 0 to len/2

for i in range(0, int(len(str)/2)):

if str[i] != str[len(str)-i-1]:

return False

return True

# main function

s = "malayalam"

ans = isPalindrome(s)

if (ans):

print("Yes")

else:

print("No")

2)

# Python program to find the factorial of a number provided by the user.

# change the value for a different result

num = 7

# To take input from the user


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

factorial = 1

# check if the number is negative, positive or zero

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

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

factorial = factorial*i

print("The factorial of",num,"is",factorial)

3)

# Python Program to find the area of triangle

a=5

b=6

c=7

# Uncomment below to take inputs from the user

# a = float(input('Enter first side: '))

# b = float(input('Enter second side: '))

# c = float(input('Enter third side: '))


# calculate the semi-perimeter

s = (a + b + c) / 2

# calculate the area

area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

print('The area of the triangle is %0.2f' %area)

5)

# Multiplication table (from 1 to 10) in Python

num = 12

# To take input from the user

# num = int(input("Display multiplication table of? "))

# Iterate 10 times from i = 1 to 10

for i in range(1, 11):

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

6)

# Program make a simple calculator

# This function adds two numbers

def add(x, y):

return x + y

# This function subtracts two numbers


def subtract(x, y):

return x - y

# This function multiplies two numbers

def multiply(x, y):

return x * y

# This function divides two numbers

def divide(x, y):

return x / y

print("Select operation.")

print("1.Add")

print("2.Subtract")

print("3.Multiply")

print("4.Divide")

while True:

# take input from the user

choice = input("Enter choice(1/2/3/4): ")

# check if choice is one of the four options

if choice in ('1', '2', '3', '4'):

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))

if choice == '1':

print(num1, "+", num2, "=", add(num1, num2))

elif choice == '2':

print(num1, "-", num2, "=", subtract(num1, num2))

elif choice == '3':

print(num1, "*", num2, "=", multiply(num1, num2))

elif choice == '4':

print(num1, "/", num2, "=", divide(num1, num2))

# check if user wants another calculation

# break the while loop if answer is no

next_calculation = input("Let's do next calculation? (yes/no): ")

if next_calculation == "no":

break

else:

print("Invalid Input")

7)to find greater number in the list

#You are supposed to enter number,

#from which the grestest number will be


#printed as a result

list1=list()

temp= int(input(print('How many number you want to enter')))

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

temp1=int(input(print('enter the number')))

list1.append(temp1)

list1.sort()

print('the largest number is:',max(list1))

print('the largest number is:', list1[-1])

8)tO FIND THE ARMSTRONG NUMBER

#What is an Armstrong number?

#1234 = 1^4 + 2^4 + 3^4 + 4^4 = 1234 it is not an armstrong number.

temp= int(input(print('enter the number you want to check for armstrong')))

temp_copy=temp

temp2=str(temp)

length=len(temp2)

print(length)

total=0

for i in range(0,length):

digits=temp%10

print(digits)

total=total+digits**length

temp=temp//10

print(total)
if total==temp_copy:

print ("the number is armstrong")

else:

print ("the number is not armstrong")

8)

TO FIND THE prime NUMBER

prime = True

temp = int(input('enter the number you want to check for prime'))

if temp > 1:

for i in range(2,temp-1):

if(temp % i == 0):

prime = False

print(prime)

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

prime = True

if num>1:

for i in range(2,num):

if(num%i==0):

prime = False

if prime:

print(num,"is Prime number.")

else:

print(num,"is not a Prime number.")

You might also like