Computer Science, asked by brajkishormullick, 3 months ago

Write a python program to find maximum among N numbers in a tuple​

Answers

Answered by anindyaadhikari13
1

Answer:

The given question can be solved in two ways.

1. Using function.

n=int(input("How many elements?? "))

print("Enter them..")

l=[]

for i in range(n):

   l.append(int(input(">> ")))

t=tuple(l)

print("Given tuple:",t)

print("Largest element in this tuple is:",max(t))

max(): The max() function finds out the largest number in the tuple. This problem is solved using max() function.

2. Using user-defined logic.

n=int(input("How many elements?? "))

print("Enter them..")

l=[]

for i in range(n):

   l.append(int(input(">> ")))

t=tuple(l)

print("Given tuple:",t)

max=t[0]

for i in t:

   if i>max:

       max=i

print("Largest element in this tuple is:",max)

We will assume that the largest number is present at first index. Now, we will loop through all the elements. If any number is found greater than max, then we will store the number in max variable. At last, max is displayed on the screen.

Note: As tuples are immutable i.e., they cannot be modified, so the entered numbers are stored in list and then the list is converted to tuple.

Refer to the attachment.

•••♪

Attachments:
Similar questions