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

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad