You are given a positive integer X. Write a simple program to find the second largest digit in the given number X.
Question
You are given a positive integer X. Write a simple program to find the second largest digit in the given number X.
Solution
Sure, here is a simple Python program that finds the second largest digit in a given positive integer X:
def second_largest_digit(X):
# Convert the integer into a list of its digits
digits = [int(d) for d in str(X)]
# Sort the list in descending order
sorted_digits = sorted(digits, reverse=True)
# Remove the largest digit from the list
sorted_digits.remove(sorted_digits[0])
# The second largest digit is now the first element in the list
second_largest = sorted_digits[0]
return second_largest
# Test the function
X = 123456789
print(second_largest_digit(X)) # Output: 8
This program works by first converting the integer into a list of its digits. It then sorts this list in descending order and removes the first element (which is the largest digit). The second largest digit is now the first element in the list.
Similar Questions
Single File Programming QuestionProblem StatementYou are given a positive integer X. Write a simple program to find the second largest digit in the given number X.Example 1:Input: X = 9765Output: 7Explanation: The second largest digit in 9765 is 7.Example 2:Input: X = 11Output: -1Explanation: If there is no second-largest digit found in the given number, print -1.Note: This question was asked in the Wipro coding test.Input format :The input consists of a positive integer X.Output format :The output prints an integer, representing the second largest digit in the given number X.If there is no second largest digit, the output prints "-1".Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:500 ≤ X ≤ 106Sample test cases :Input 1 :99856Output 1 :8Input 2 :555Output 2 :-1Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
In the given numbers, if in each number first digit is replaced by the third digit, second digit is replaced by the first digit and third digit is replaced by the second digit then which number will be the biggest ?653 749 872 933 541Options933749653872
write a c program Find the largest number among the three numbers.
You are given a positive integer . You can perform the following operation on it atmost once :Choose a single digit from the number and change it to any digit you want from to .Your task is to generate the largest number such that and is a multiple of . If no such exists, print .Input FormatFirst line of input contains an integer , denoting the number of testcases lines of input follow- where each line contains a number .Constraints There are no leading zeroes in denotes the number of digits in the number .Please note that might not fit in the Long Long Int rangeOutput FormatFor each test case - print the value of if it exists; else print .Sample Input 0334999889Sample Output 084999-1Explanation 0In the test case, change the digit to to obtain the largest number possibleIn the test case, changing any digit in would lead to a lesser number so it's optimal to leave it as it is - as is already divisble by In the test case, it is impossible to generate a such that it is greater than or equal to and divisible by by changing atmost one digit of . Hence, we print .Sample Input 118292849184018401939169Sample Output 18792849184018401939169Explanation 1By changing the digit of to , we get the largest number possible which is divisible by .
Which is the largest possible two digit number which divides 32472?99446688
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.