Find 2nd largest digit in a given number

Write a C program to Find 2nd largest digit in a given number. or Write a program to Find 2nd largest digit in a given number in C.

Program in C

Code:

/*Write a C program to Find 2nd largest digit in a given number. or Write a program to Find 2nd largest digit in a given number Using C*/

#include<stdio.h>
int main ()
{
int num, reminder, Largest= 0,Sec_Largest=0;
printf("Enter the Number :");
scanf("%d",&num);
while (num > 0)
    {
reminder = num % 10;
if (Largest < reminder)
{
    Sec_Largest=Largest;
       Largest = reminder;
}
else if(reminder>=Sec_Largest)
        Sec_Largest=reminder;
num = num / 10;
    }
printf("The Second Largest Digit is %d", Sec_Largest);
return 0;
}

Input/Output:
Enter the Number :
845319
The Second Largest Digit is 8

Write a C++ program to Find 2nd largest digit in a given number. or Write a program to Find 2nd largest digit in a given number in C++.


Program in C++

Code:

/*Write a C++ program to Find 2nd largest digit in a given number. or Write a program to Find 2nd largest digit in a given number using C++*/

#include<iostream>
using namespace std;
int main ()
{
int num, reminder, Largest= 0,Sec_Largest=0;
cout<<"Enter the Number :";
cin>>num;
while (num > 0)
    {
reminder = num % 10;
if (Largest < reminder)
{
    Sec_Largest=Largest;
       Largest = reminder;
}
else if(reminder>=Sec_Largest)
        Sec_Largest=reminder;
num = num / 10;
    }
cout<<"The Second Largest Digit is "<< Sec_Largest;
return 0;
}

Input/Output:
Enter the Number:78451
The Second Largest Digit is 7

Write a JAVA program to Find 2nd largest digit in a given number. or Write a program to Find 2nd largest digit in a given number in Java.

Program in Java

Code:

/*Write a JAVA program to Find 2nd largest digit in a given number. or Write a program to  Find 2nd largest digit in a given number using Java*/

import java.util.Scanner;
public class SecondlargestNumber {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num, reminder, Largest= 0,Sec_Largest=0;
System.out.println("Enter the Number :");
num=cs.nextInt();
while (num > 0)
    {
reminder = num % 10;
if (Largest < reminder)
{
    Sec_Largest=Largest;
       Largest = reminder;
}
else if(reminder>=Sec_Largest)
        Sec_Largest=reminder;
num = num / 10;
    }

System.out.println("The Second Largest Digit is "+ Sec_Largest);
cs.close();
}
}

Input/Output:
Enter the Number :
4562
The Second Largest Digit is 5

Write a PYTHON to Find 2nd largest digit in a given number. or Write a program to Find 2nd largest digit in a given number in Python.

Program in Python

Code:

'''Write a Python program to Find the 2nd largest digit in a given number. or Write a program to Find 2nd largest digit in a given number using Python '''

print("Enter the Number :")
num=int(input())
Largest=0
Sec_Largest=0
while num > 0:
    reminder=num%10
    if Largest<reminder:
        Sec_Largest = Largest
        Largest = reminder
    elif reminder >= Sec_Largest:
        Sec_Largest = reminder
    num =num // 10
print("The Second Largest Digit is :", Sec_Largest)

Input/Output:
Enter the Number :
5478
The Second Largest Digit is: 7


C/C++/Java/Python Practice Question

Post a Comment

0 Comments