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

LIST Programs

The document outlines various programming tasks related to array manipulation, including finding averages, largest and smallest elements, counting positive and negative numbers, and checking for prime numbers. It provides sample code snippets for each task, demonstrating how to implement the functionality in Python. Additionally, it includes revisions on identifying prime, Armstrong, and Harshad numbers.
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)
5 views7 pages

LIST Programs

The document outlines various programming tasks related to array manipulation, including finding averages, largest and smallest elements, counting positive and negative numbers, and checking for prime numbers. It provides sample code snippets for each task, demonstrating how to implement the functionality in Python. Additionally, it includes revisions on identifying prime, Armstrong, and Harshad numbers.
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
You are on page 1/ 7

Programs On LIST

1. Find the average of all elements in an array.


2. Find the largest element in an array.
3. Find the smallest element in an array.
4. Check if a given element exists in the array.
5 Count the number of positive and negative elements in an
array.
6. Calculate the difference between the maximum and
minimum elements in an array.
7. Find the sum of all prime numbers in an array.
Revision
a. Prime number or not
b. ArmStrong number or not
c.Harshad number or not

1. Find the number of all elements in an array.

ls = list(map(int,input().split()))
count = 0
for i in ls:
count += 1

print("Number of elements present in an array: ",count)

2. Find the sum of all elements in an array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]

#using sum()
print("Sum: ",sum(a))

#adding each value


sumOfValues = 0
for i in a:
sumOfValues += i
noOfValues += 1

3. Find the average of elements in an array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]

sumOfValues = 0 #adding each value from the list


noOfValues = 0 # Counting no of values in the list

for i in a:
sumOfValues += i
noOfValues += 1

print("sumOfValues: ",sumOfValues)

#Average = sum of values/number of values


print("Average: ", sumOfValues/noOfValues)

4. Find the largest element in an array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]

#using max() function.


print(max(a))

#OR

#checking each element in the list


largEle = a[0]
for i in range(1,n):
if a[i] > largEle:
largEle = a[i]
print(largEle)

5. Find the smallest element in an array.


n = int(input())
a = list(map(int,input().strip().split()))[:n]

#using min() function.


print(min(a))

#OR

#checking each element in the list


smallEle = a[0]
for i in range(1,n):
if a[i] < smallEle:
smallEle = a[i]
print(smallEle)
6. Count the number of even elements in an array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]

countEven = 0
for i in a:
if i%2 == 0:
countEven += 1
print(countEven)

7. Count the number of odd elements in an array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]

countOdd = 0
for i in a:
if i%2 != 0:
countOdd += 1
print(countOdd)

8. Find the second largest element in an array.

def findSecondLarge(ls):
max = ls[0]
smax = 0

for element in ls:


if element > max:
smax = max
max = element
if element > smax and element < max:
smax = element
print(smax)
ls = [1000,77,394,10,23,10,89]
findSecondLarge(ls)

Output: 394

OR

def findSecondLarge(ls):
ls.pop(ls.index(max(ls)))
return max(ls)
ls = [1000,77,394,10,23,10,89]
print(findSecondLarge(ls))

9. Check if a given element exists in the array.

ls = list(map(int,input().split()))
k = int(input("Enter k value: "))

for i in ls:
if i == k:
print("Yes")
break
else:
print("No")
10. Calculate the difference between the maximum and
minimum elements in an array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]
min = a[0]
max = a[0]
for i in range(1,n):
if a[i] < min:
min = a[i]
if a[i] > max:
max = a[i]
print(max - min)

11. Count the number of positive and negative elements in an


array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]
numberOfPositive = 0
numberOfNegative = 0

for value in a:
if value < 0:
numberOfNegative += 1
else:
numberOfPositive += 1
print("Number of positive value: ",numberOfPositive)
print("Number of negative value: ",numberOfNegative)
12. Find the sum of all prime numbers in an array.

n = int(input())
a = list(map(int,input().strip().split()))[:n]
numberOfPositive = 0
numberOfNegative = 0
sumOfPrimes = 0
for value in a:
c=0
p = value
for i in range(1,p+1):
if p%i == 0:
c += 1
if c == 2:
sumOfPrimes += p
print(sumOfPrimes)

You might also like