Problem Description
The program takes a list and prints the largest number in the list.
Problem Solution
1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. Sort the list in ascending order.
4. Print the last element of the list.
5. Exit.
Program/Source Code
Here is source code of the Python Program to find the largest number in a list. The program output is
also shown below.
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Largest element is:",a[n-1])
Program Explanation
1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. The list should then be sorted.
4. Then the last element of the list is printed which is also the largest element of the list.
2-This is a Python Program to find the second largest number in
a list.
Problem Description
The program takes a list and prints the second largest number in the list.
Problem Solution
1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. Sort the list in ascending order.
4. Print the second last element of the list.
5. Exit.
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Second largest element is:",a[n-2])
Program Explanation
1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. The list should then be sorted.
4. Then the last element of the list is printed which is also the largest element of the list.
3-This is a Python Program to print the largest even and largest
odd number in a list.
Problem Description
The program takes in a list and prints the largest even and largest off number in it.
Problem Solution
1. Take in the number of elements to be in the list from the user.
2. Take in the elements from the user using a for loop and append to a list.
3. Using a for loop, get the elements one by one from the list and check if it odd or even and append
them to different lists.
4. Sort both the lists individually and get the length of each list.
5. Print the last elements of the sorted lists.
6. Exit.
Program/Source Code
Here is source code of the Python Program to print the largest even and largest odd number in a list.
The program output is also shown below.
n=int(input("Enter the number of elements to be in the list:"))
b=[]
for i in range(0,n):
a=int(input("Element: "))
b.append(a)
c=[]
d=[]
for i in b:
if(i%2==0):
c.append(i)
else:
d.append(i)
c.sort()
d.sort()
count1=0
count2=0
for k in c:
count1=count1+1
for j in d:
count2=count2+1
print("Largest even number:",c[count1-1])
print("Largest odd number",d[count2-1])
Program Explanation
1. User must enter the number of elements to be in the list.
2. User must then enter the elements of the list one by one which is appended to the list.
3. The for loop is used to traverse through the list and obtain the elements one by one.
4. The if statement checks whether the element is odd or even.
5. The odd elements are appended to another list and the even elements are appending to another list.
6. Both the lists are then sorted.
7. The individual lengths of both the lists is obtained using for loops.
8. The last element of the sorted lists which is the largest odd and largest even number is printed.
4-This is a Python Program to print the sum of negative
numbers, positive even numbers and positive odd numbers in a
given list.
Problem Description
The program prints the sum of negative numbers, positive even numbers and positive odd numbers in a
given list..
Problem Solution
1. Take in the number of elements to be in the list from the user.
2. Take in the elements from the user using a for loop and append to a list.
3. Using a for loop, get the elements one by one from the list and check if it is positive or negative.
4. If it is positive, check if it is odd or even and find the individual sum.
5. Find the individual sum of negative numbers.
Here is source code of the Python Program to print the sum of negative numbers, positive even
numbers and positive odd numbers in a given list. The program output is also shown below.
n=int(input("Enter the number of elements to be in the list:"))
b=[]
for i in range(0,n):
a=int(input("Element: "))
b.append(a)
sum1=0
sum2=0
sum3=0
for j in b:
if(j>0):
if(j%2==0):
sum1=sum1+j
else:
sum2=sum2+j
else:
sum3=sum3+j
print("Sum of all positive even numbers:",sum1)
print("Sum of all positive odd numbers:",sum2)
print("Sum of all negative numbers:",sum3)
Program Explanation
1. User must enter the number of elements to be in the list.
2. User must then enter the elements of the list one by one which is appended to the list.
3. The for loop is used to traverse through the list and obtain the elements one by one.
4. The if statement checks whether the element is positive or negative.
5. If it is positive, another if statement checks if it is odd or even.
6. The sum of even elements is stored in sum1 and the sum of odd elements is stored in sum2.
7. The sum of negative numbers is stored in sum3.
8. All the sums are respectively printed.
5-This is a Python Program to find the sum of series: 1 + 1/2 +
1/3 + ….. + 1/N.
Problem Description
The program takes in the the number of terms and finds the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N.
Problem Solution
1. Take in the number of terms to find the sum of the series for.
2. Initialize the sum variable to 0.
3. Use a for loop ranging from 1 to the number and find the sum of the series.
4. Print the sum of the series after rounding it off to two decimal places.
5. Exit.
Program/Source Code
Here is source code of the Python Program to find the sum of series: 1 + 1/2 + 1/3 + ….. + 1/N. The
program output is also shown below.
n=int(input("Enter the number of terms: "))
sum1=0
for i in range(1,n+1):
sum1=sum1+(1/i)
print("The sum of series is",round(sum1,2))
Program Explanation
1. User must enter the number of terms to find the sum of.
2. The sum variable is initialized to 0.
3. The for loop is used to find the sum of the series and the number is incremented for each iteration.
4. The numbers are added to the sum variable and this continues till the value of i reaches the number
of terms.
5. Then the sum of the series is printed.