Programming Examples
Java program to pass an integer as argument and print the second largest digit
Write a program to pass an integer as argument and print the second largest digit
Solution
class SecondLargestDigit
{
public static void main(int n)
{
int i,d,lar=0,slar=0;
for(i=n;i>0;i/=10)
{
d=i%10;
if(lar==0)
lar=d;
if(d>lar)
lar=d;
}
for(i=n;i>0;i/=10)
{
d=i%10;
if(d!=lar)
{
if(slar==0)
slar=d;
if(d>slar)
slar=d;
}
}
System.out.println(“Second Largest digit:”+slar);
}
}
Output