0% found this document useful (0 votes)
12 views

Python 10 Example

Uploaded by

prad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python 10 Example

Uploaded by

prad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1) Python program to add two numbers.

# Python program to add two numbersnum1 = 18


num2 = 7# Adding two numbers
sum = num1 + num2# printing values
print(f“Sum of {num1} and {num2} is {sum}”)

Output:
Sum of 15 and 12 is 27

2) Python program to check if a string is palindrome or


not.
mystr = “malayalam”# reversing a string
rev = mystr[::-1]if mystr == rev:
print(“Yes”)
else:
print(“No”)

Output:
Yes

3) Python program for finding factorial of a number.

Factorial of a number is the multiplication of the first n


integers. Factorial can be found only for positive integer.

Example: Factorial of 4 is 4*3*2*1 = 24


# function to find factorial of given number
def factorial(n):
if (n==1 or n==0):
return 1
else:
return n * factorial(n — 1) #recursion

num = 6;
print(“Factorial of”,num,”is”,factorial(num))

Output:
Factorial of 6 is 720

4) Python program for finding sum of first n natural


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

if num < 0:
print("Negative numbers are not allowed")
else:
sum = 0
i = num
# finding the sum
while(i > 0):
sum += i
i -= 1
print(f"sum of first {num} natural number is",sum)

Output:
Enter a number: 8
sum of first 8 natural number is 36

5) Python program for counting the digits in a number.


num=int(input("Enter number: "))
count=0
i = num# count the digits
while(i>0):
count=count+1
i=i//10
print(f"The number of digits in {num}:",count)

Output:
Enter number:8516
The number of digits in 8516: 4

6) Python program for printing Multiplication tables.


num = int(input ("Enter a number for the multiplication table:
"))print (num, "Tables\n")# for loop to iterate 10 times
for i in range(1, 11):
print (num, 'x', i, '=', num * i)

Output:
Enter a number for the multiplication table: 17
17 Tables17 x 1 = 17
17 x 2 = 34
17 x 3 = 51
17 x 4 = 68
17 x 5 = 85
17 x 6 = 102
17 x 7 = 119
17 x 8 = 136
17 x 9 = 153
17 x 10 = 170

7) Python program for converting Kilometers to miles.

1 Kilometer is equal to 0.621371 Miles


# Function for converting kilometers into milesdef
convertKilometre(km):
conversion_ratio= 0.621371
miles = km * conversion_ratio
print ("The speed in Miles: ", miles)

km = float (input ("Enter the speed in Kilometre: "))


convertKilometre(km)

Output:
Enter the speed in Kilometre: 57.6
The speed in Miles: 35.790969600000004
8) Python program for finding largest number in a list.
# Python program to find largest number in a list

mylist = [101, 62, 66, 45, 99, 18, 266, 18]maxi = mylist[0] #
initializing maximum value
for i in mylist:
if i > maxi:
maxi = i

# printing the largest number


print("Largest number is:", maxi)

Output:
Largest number is: 266

9) Python program for finding n-th Fibonacci number .

The fibonacci sequence is the series of numbers in which any


given number is the sum of previous two number. That
is F(n) = F(n-1) + F(n-2). Here we start with F(0)=0,
F(1)=1

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233 ……..


# Function for finding nth fibonacci value

def Fib(n):
if n<= 0:
print("Incorrect input")
# First number is 0
elif n == 1:
return 0
# Second number is 1
elif n == 2:
return 1
else:
return Fib(n-1)+Fib(n-2)
n = int(input("Enter a number: "))
fib_value = Fib(n)
print(f"{n}th Fibonacci number is {fib_value}")

Output:
Enter a number: 10
10th Fibonacci number is 34

10) Python program for checking a number is prime or


not.
# function for checking prime numbers
def Prime(n):
# Checking that n is more than 1
if n > 1:
# Iterating over the n
for i in range(2, int(n/2) + 1):
# If n is divisible or not
if (n % i) == 0:
print(n, "is not a prime number")
break
else:
print(n, "is a prime number")
# If n is 1
else:
print(n, "is not a prime number")

# Taking an input from the user


num = int(input("Enter an input number:"))# Printing result
Prime(num)

Output:
Enter an input number:8
8 is not a prime number

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy