Write C# program to swap first and last digit of a 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, last, first, temp, count = 0;
  12. double swap;
  13.  
  14. // Reading number
  15. Console.Write("Enter any number: ");
  16. num = Convert.ToInt32(Console.ReadLine());
  17.  
  18. temp = num;
  19. last = temp % 10;
  20. count = (int)Math.Log10(temp);
  21.  
  22. while (temp >= 10)
  23. {
  24. temp /= 10;
  25. }
  26. first = temp;
  27. swap = (last * Math.Pow(10, count) + first) + (num - (first * Math.Pow(10, count) + last));
  28.  
  29. Console.WriteLine("Last Digit:" + last);
  30.  
  31. Console.WriteLine("First Digit:" + first);
  32.  
  33. Console.WriteLine(num + " is swapped to " + swap);
  34.  
  35. Console.ReadLine();
  36. }
  37. }

Result

C# language techstudy

     





Related Topics




Popular Posts