In this example we’ll learn How to find first and last digit of any number in C#.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
static void Main(string[] args) { int num, last; // Reading number Console.Write("Enter any number: "); num = Convert.ToInt32(Console.ReadLine()); last = num % 10; Console.WriteLine("The last digit of entered number: " + last); while (num >= 10) { num = num / 10; } Console.WriteLine("The first digit of entered number: " + num); Console.ReadLine(); } |
Output: