A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write C# program to swap first and last digit of a number
Q:

Write C# program to swap first and last digit of a number

0

Write C# program to swap first and last digit of a number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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, last, first, temp, count = 0;
        double swap;
 
        // Reading number
        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());
 
        temp = num;
        last = temp % 10;
        count = (int)Math.Log10(temp);
 
        while (temp >= 10)
        {
            temp /= 10;
        }
        first = temp;
        swap = (last * Math.Pow(10, count) + first) + (num - (first * Math.Pow(10, count) + last));
 
        Console.WriteLine("Last Digit:" + last);
 
        Console.WriteLine("First Digit:" + first);
 
        Console.WriteLine(num + " is swapped to " + swap);
 
        Console.ReadLine();
    }
}

Result:

Enter any number: 123456

Last Digit:6

First Digit:1

123456 is swapped to 623451

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now