C# Program to find the largest digit of a number


In this program, You will learn how to find the largest digit of a number in C#.


12345 => 5

48493 => 9

Example: How to find the largest digit of a number in C#

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int n, r, ld = 0;
		Console.Write("Enter a number:");
		n = Convert.ToInt32(Console.ReadLine());

		while (n > 0)
		{
			r = n % 10;
			if (ld < r)
			{
				ld = r;
			}
			n = n / 10;
		}
		Console.WriteLine("Largest digit:" + ld);
	}
}

Output:

Enter a number:2341
Largest digit:4