Write C# program to find the sum of first and last digit of any number

Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
public class csharpExercise
{
    static void Main(string[] args)
    {
 
        int num, sum = 0, firstDigit, lastDigit;
 
        // Reading number
        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());
 
        lastDigit = num % 10;
 
        firstDigit = num;
 
        while (num >= 10)
        {
            num = num / 10;
        }
        firstDigit = num;
 
        //Finding sum of first and last digit
        sum = firstDigit + lastDigit;
 
        Console.WriteLine("Sum of first and last digit: "+ sum);
 
        Console.ReadLine();
    }
}

Result

Write C# program to find the sum of first and last digit of any number
Write C# program to find the sum of first and last digit of any number

Leave a Comment