C# Program to find the sum of first and last digit of a number


In this program, You will learn how to find the sum of first and last digit of a number in C#.


1234 = 1 + 4 => 5

Example: How to find the sum of first and last digit of a number in C#.

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

		ld = n % 10;

		while (n > 0)
		{
			r = n % 10;
			rev = rev * 10 + r;
			n = n / 10;
		}

		fd = rev % 10;
		sum = fd + ld;

		Console.WriteLine("Sum of first and last digit:" + sum);
	}
}

Output:

Enter a number:3424
Sum of first and last digit:7