0% found this document useful (0 votes)
14 views11 pages

Programs

Uploaded by

23nu1a0494
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)
14 views11 pages

Programs

Uploaded by

23nu1a0494
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

Program 1:

Python program to find Maximum of Two Numbers:

def maximum(x,y):
print("Maximum of Two Number is")
if x>=y:
return x
else:
return y

Program 2:
Python program to check whether the given number is even or not.

def even_odd(x):
if x%2==0:
print("The Number Is Even")
return x
else:
print("The Number Is Odd")
return x

Program 3:
Python program to find out the average of a set of integers

num=int(input("enter no.of values: "))


// enter no.of values: 5
>>>
sum=0
for i in range(num):
x=int(input("enter number: "))
sum=sum+x
...
enter number: 5
enter number: 7
enter number: 8
enter number: 3
enter number: 6
>>>
print(“the sum of numbers is: ”, sum)
29
print(“the average of numbers is: “, sum/num)
5.8

Program 4
Python program to check whether the given integer is a multiple of both 5 and 7

def mul7and5(x):
if((x%5==0) and (x%7==0)):
print("The Number is Multiple of both 5 and 7")
return x
else:
print("The Number is not Multiple of both 5 and 7")
return x
Program 5
Python program to display the given integer in reverse manner

def reverse_number(num):
rev = 0
while(num>0):
digit = num%10
rev = (rev*10)+digit
num=int(num/10)
print("The Reverse of the Number is ")
return rev

547
digit = 547%10 = 7 rev = (0*10)+ digit = 7 number = number//10 = 54
rev = 7
digit = 54%10 = 4 rev = (7*10)+ digit = 74 number = number // 10 = 5
rev 74
digit = 5%10 = 5 rev = (74*10)+digit = 745 number = number // 10 = 0
rev 745

Program 6
Python program to find the sum of the digits of an integer using while loop

def digits_sum(num):
sum=0
while(num!=0):
digit = num%10
sum = sum+digit
num=num//10
print("Sum of Digits is: ")
return sum
Program 7
Python program to check whether the given integer is a prime number or not

def prime_number(n):
isprime = 1 #assuming that number is prime
for i in range(2,n):
if(n%i==0):
isprime = 0
break
if(isprime==1):
print("The Number Is A Prime Number")
return n
else:
print("The Number Is Not A Prime Number")
return n

Program 8
Python program to calculate the Simple Interest

def simple_interest(p,t,r):
print("The Principal Amount is", p)
print("The Time Period is ", t)
print("The rate Of Interest is ", r)
si=(p*t*r)/100
print("The Simple Interest is ", si)
Program 9
Python program to generate the prime numbers from 1 to N

def pr(n):
print("The Prime Numbers from 1 to ", n)
for i in range(1,n+1):
c=0
for j in range(1,i+1):
if(i%j==0):
c=c+1
if(c==2):
print(i) // print( i,end = ‘ ‘)

Program 10
Python program to find the factorial of a number using recursion

def fact(n):
if n==1:
f=1
else:
f = n * fact(n-1)
return f
Program 11
Python program to implement linear search

numbers = [4,2,7,1,8,3,6]
def list(x):
f=0
for i in range(len(numbers)):
if (x==numbers[i]):
print("Successful search, the element is found at position", i)
f=1
break
if(f==0):
print("Oops search unsuccessful")

Program 12
Python program to print odd numbers in the List

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


for num in list1:
if (num%2 !=0):
print(num)

Program 13
Python program to print total number of odd numbers in the List

numbers = [8,3,1,6,2,4,5,9]
count = 0
for i in range(len(numbers)):
if(numbers[i]%2!=0):
count = count+1
>>>
print("The number of odd numbers in the list are: ", count)
Program 14
Python program to find largest number in a list using build in function

list1 = [10,20,99,45,4]
list1.sort()
print("Largest element in the list1 is ", list1[-1])

Program 15
Python program to find largest number in a list without using build in function

numbers = [3,8,1,7,2,9,5,4]
big = numbers[0]
position = 0
for i in range(len(numbers)):
if (numbers[i]>big):
big = numbers[i]
position = i
>>>
print("The largest element is ",big," which is found at position ",position)

Program 16
Python program to check whether a string is Pallindrome or not

def pallindrome(s):
if (s==s[::-1]):
print(s, "Is a Pallindrome")
else:
print(s, "Is not a Pallindrome")
Program 17
Python program to implement Matrix Addition

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
>>>
Y = [[5,8,1],
[6,7,3],
[4,5,9]]
>>>
result = [[0,0,0],
[0,0,0],
[0,0,0]]
>>>
for i in range(len(X)):
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
>>>
for r in result:
print(r)

Program 18
Python program to implement Matrix Multiplication

X = [[8,5,1],
[9 ,3,2],
[4 ,6,3]]
>>>
Y = [[8,5,3],
[9,5,7],
[9,4,1]]
>>>
result = [[0,0,0],
[0,0,0],
[0,0,0]]
>>>
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
>>>
for r in result:
print(r)

Program 19
Python program to display the Sum of n numbers using a list

numbers = []
num = 5
for n in range(num):
x = int(input('Enter number '))
numbers.append(x)
>>>
print(numbers)
>>>
print("Sum of numbers in the given list is :", sum(numbers))
Program 20
Python program to find the given integer is Armstrong Number or not
153 = 1*1*1 + 5*5*5 + 3*3*3 = 153 Armstrong Number
120 = 1*1*1 + 2*2*2 + 0*0*0 = 9 Not an Armstrong Number

num = 153
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
>>>
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Program 21
Python program for sum of square of first N natural numbers
5 = 5*5 + 4*4 + 3*3 + 2*2 + 1*1 = 55

N=5
sumVal = 0
for i in range(1, N+1):
sumVal += (i*i)
>>>
print("Sum of squares = ", sumVal)

Program 22
Python program for Fibonacci Series

def fib(n):
a=2
b=3
print(a)
print(b)
for i in range(2,n):
c=a+b
print(c)
a=b
b=c

You might also like