Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programs On Operators

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

ADP PROGRAMS SREC

A) Read a list of numbers and write a program to check whether a particular element is present or
not using membership operators.

Program:

a = 10

b = 20

list = [1, 2, 3, 4, 5 ];

if ( a in list ):

print ("Line 1 - a is available in the given list")

else:

print ("Line 1 - a is not available in the given list")

if ( b not in list ):

print ("Line 2 - b is not available in the given list")

else:

print ("Line 2 - b is available in the given list")

a=2

if ( a in list ):

print( "Line 3 - a is available in the given list")

else:

print ("Line 3 - a is not available in the given list")

Output:

Line 1 - a is not available in the given list

Line 2 - b is not available in the given list

Line 3 - a is available in the given list

1
ADP PROGRAMS SREC
b. Read your name and age and write a program to display the year in which you will turn 100 years
old

name = input("What is your name: ")


age = int(input("How old are you: "))
year = str((2022 - age)+100)
print(name + " will be 100 years old in the year " + year)

Output
What is your name: Dev

How old are you: 3

Dev will be 100 years old in the year 2119

2
ADP PROGRAMS SREC
c. Read radius and height of a cone and write a program to find the volume of a cone.

Program:

height=38

radius=35

pie=3.14285714286

volume=pie*(radius*radius)*height/3

print("volume of the cone="+str(volume))

Output:

volume of the cone= 48766.666666711004

3
ADP PROGRAMS SREC
d. Write a program to compute distance between two points taking input from the user (Hint: use
Pythagorean theorem)

Program:

x1=int(input("enter x1 : "))
x2=int(input("enter x2 : "))
y1=int(input("enter y1 : "))
y2=int(input("enter y2 : "))
result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5)
print("distance between",(x1,x2),"and",(y1,y2),"is : ",result)

Output

enter x1 : 8

enter x2 : 6

enter y1 : 9

enter y1 : 5

distance between (8,6) and (9,5) is : 4.47213595

4
ADP PROGRAMS SREC
2.CONTROL STRUCTURES
A).Read your email id and write a program to display the no of vowels,
consonants, digits and white spaces in it using if…elif…else statement.

def countCharacterType(str):
vowels = 0
consonant = 0
specialChar = 0
digit = 0
for i in range(0, len(str)):
ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or
(ch >= 'A' and ch <= 'Z') ):
# To handle upper case letters
ch = ch.lower()
if (ch == 'a' or ch == 'e' or ch == 'i'
or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1

elif (ch >= '0' and ch <= '9'):


digit += 1
else:
specialChar += 1
print("Vowels:", vowels)

5
ADP PROGRAMS SREC
print("Consonant:", consonant)
print("Digit:", digit)
print("Special Character:", specialChar)
# Driver function.
str = "geeks for geeks121"
countCharacterType(str)
Output:
Vowels: 7
Consonant: 12
Digit: 3
Special Character: 4

6
ADP PROGRAMS SREC
B) write a program to create and display dictionary by storing the antonyms
of words. find the antonym of a particular word given by the user from the
dictionary using while loop in python
Program
print('enter word from following words ')
no ={'right':'left','up':'down','good':'bad','cool':'hot','east':'west'}
for i in no.keys():
print(i)
y = input()
if y in no :
print('antonym is ', no[y])
else:
print('not in the list given :(,sowwy ')
Output:
enter word from following words
right
up
good
cool
east
good
antonym is bad

7
ADP PROGRAMS SREC
C)Write a Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….
+ n/n!. (Input :n = 5, Output : 2.70833)

Program
# Python code to find smallest K-digit
# number divisible by X

def sumOfSeries(num):

# Computing MAX
res = 0
fact = 1

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


fact *= i
res = res + (i/ fact)

return res
n=5
print("Sum: ", sumOfSeries(n))

Output:
Sum: 2.70833

8
ADP PROGRAMS SREC
D)In number theory, an abundant number or excessive number is a number
for which the sum of its proper divisors is greater than the number itself.
Write a program to find out, if the given number is abundant. (Input: 12,
Sum of divisors of 12 = 1 + 2 + 3 + 4 + 6 = 16, sum of divisors 16 > original
number 12)

# An Optimized Solution to check Abundant Number


# in PYTHON
import math

# Function to calculate sum of divisors


def getSum(n) :
sum = 0

# Note that this loop runs till square root


# of n
i=1
while i <= (math.sqrt(n)) :
if n%i == 0 :

# If divisors are equal,take only one


# of them
if n//i == i :
sum = sum + i
else : # Otherwise take both
sum = sum + i
sum = sum + (n // i )
i=i+1

# calculate sum of all proper divisors only


sum = sum - n
return sum

# Function to check Abundant Number


def checkAbundant(n) :

# Return true if sum of divisors is greater


# than n.
if (getSum(n) > n) :
return 1
else :

9
ADP PROGRAMS SREC
return 0

# Driver program to test above function */


if(checkAbundant(12) == 1) :
print ("YES")
else :
print ("NO")

if(checkAbundant(15) == 1) :
print ("YES")
else :
print ("NO")

Output:
YES
NO

10
ADP PROGRAMS SREC

3: LIST
a). Read a list of numbers and print the numbers divisible by x but not by y
(Assume x = 4 and y = 5).
def result(N):
# iterate from 0 to N
for num in range(N):
# Short-circuit operator is used
if num % 4 == 0 and num % 5 != 0:
print(str(num) + " ", end = "")
else:
pass
# Driver code
if __name__ == "__main__":
# input goes here
N = 100
# Calling function
result(N)
output:- 4 8 12 16 24 28 32 36 44 48 52 56 64 68 72 76 84 88 92 96

11
ADP PROGRAMS SREC
B). Read a list of numbers and print the sum of odd integers and even
integers from the list.(Ex: [23, 10, 15, 14, 63], odd numbers sum = 101, even
numbers sum = 24)
# Python program to count Even
# and Odd numbers in a List
NumList = []

Even_Sum = 0

Odd_Sum = 0

j=0

Number = int(input("Please enter the Total Number of List Elements: "))

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

value = int(input("Please enter the Value of %d Element : " %i))

NumList.append(value)

while(j < Number):

if(NumList[j] % 2 == 0):

Even_Sum = Even_Sum + NumList[j]

else:

Odd_Sum = Odd_Sum + NumList[j]

j = j+ 1

print("\nThe Sum of Even Numbers in this List = ", Even_Sum)

print("The Sum of Odd Numbers in this List = ", Odd_Sum)

Output:
Please enter the total number of list elements: 5
12
ADP PROGRAMS SREC
Please enter the value of 1 element:23
Please enter the value of 2 element:10
Please enter the value of 3 element:15
Please enter the value of 4 element:14
Please enter the value of 5 element:63
Sum of Even Numbers in this List=24
Sum of odd Numbers in this List =101

13
ADP PROGRAMS SREC
c. Read a list of numbers and print numbers present in odd index position.
(Ex: [10, 25, 30, 47, 56, 84, 96], The numbers in odd index position: 25 47
84).
Program:
# Python3 implementation to
# arrange odd and even numbers
arr = list()
print(end="Enter the List Size: ")
arrTot = int(input())
print(end="Enter " +str(arrTot)+ " Numbers: ")
for i in range(arrTot):
arr.append(int(input()))
print("\nNumbers at Odd Position: ")
for i in range(arrTot):
if i%2!=0:
print(end=str(arr[i]) +" ")
print()
Output:- Enter the List Size: 7
Enter the 7 number10
25
30
47
56
84
Numbers at the odd position 25 47 84

14
ADP PROGRAMS SREC
d). Read a list of numbers and remove the duplicate numbers from it. (Ex:
Enter a list with duplicate elements: 10 20 40 10 50 30 20 10 80, The unique
list is: [10, 20, 30, 40, 50, 80]
Program:
my_list = [10,20,40,10,50,30,20,10,80]
print("List Before ", my_list)
temp_list = []
for i in my_list:
if i not in temp_list:
temp_list.append(i)

my_list = temp_list
print("List After removing duplicates ", my_list)

Output:
List Before [10,20,40,10,50,30,20,10,80]
List After removing duplicates [10,20,40,50,30,80]

15

You might also like