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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. public class csharpExercise
  7. {
  8. static void Main(string[] args)
  9. {
  10.  
  11. int num, sum = 0, firstDigit, lastDigit;
  12.  
  13. // Reading number
  14. Console.Write("Enter any number: ");
  15. num = Convert.ToInt32(Console.ReadLine());
  16.  
  17. lastDigit = num % 10;
  18.  
  19. firstDigit = num;
  20.  
  21. while (num >= 10)
  22. {
  23. num = num / 10;
  24. }
  25. firstDigit = num;
  26.  
  27. //Finding sum of first and last digit
  28. sum = firstDigit + lastDigit;
  29.  
  30. Console.WriteLine("Sum of first and last digit: "+ sum);
  31.  
  32. Console.ReadLine();
  33. }
  34. }

Result

C# language techstudy

     





Related Topics




Popular Posts