Programming Examples

Java program to pass an integer as argument and print the largest as well as smallest digit


Write a program to pass an integer as argument and print the largest as well as smallest digit.

Solution

class LargestSmallest
{
	public static void main(int a)
	{
		int i,d,l=0,s=0,c=0;
		for(i=a;i>0;i=i/10)
		{
			d=i%10;
			c++;
			if(c==1)
				l=s=d;
			if(d>l)
				l=d;
			if(d<s)
				s=d;
		}
		System.out.println("Largest="+l);
		System.out.println("Smallest="+s);
	}
}
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 Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz