Article

How to find the largest digit of a number in C# and VB.Net

In this program you will learn How to find the largest digit of a number in C# and VB.Net.

Find the largest digit of a number in C# and VB.Net.

Code for how to find the largest digit of a number in C#.

using System;

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

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

Advertisement

Output

Code for how to find the largest digit of a number in VB.Net .

Imports System

Module Program
    Sub Main(args As String())
        Dim n, r As Integer, digit As Integer = 0
        Console.Write("Enter a number:")
        n = Convert.ToInt32(Console.ReadLine())

        While n > 0
            r = n Mod 10

            If digit < r Then
                digit = r
            End If

            n = n / 10
        End While

        Console.WriteLine("Largest digit:" & digit)
        Console.ReadLine()

    End Sub
End Module

Output :find the largest digit of a number in VB.Net

Advertisement

Leave a Reply

Your email address will not be published. Required fields are marked *