June 10, 2020

How to write a program in c# to swap two numbers?

Write a program in C# to swap two numbers.

How to write a program in c# to swap two numbers?

Sample Solution

C# Sharp Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class intercharge
    {
        int top_score;
        int New_score;
        int temp;
        void Swap()
        {
            Console.Write("Enter Top Score : ");
            top_score = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter New Score : ");
            New_score = Convert.ToInt32(Console.ReadLine());
            temp = top_score;
            top_score = New_score;
            New_score = temp;

        }
        void Display()
        {
Console.WriteLine("\n After swapping \n");
Console.WriteLine(" The new value of top score is :{0}",top_score);
Console.WriteLine(" The old value of top score is :{0}",New_score);
        }

        static void Main(string[] args)
        {
            intercharge obj = new intercharge();

            obj.Swap();
            obj.Display();
            Console.ReadLine();
        }
    }
}

Sample Output
Swap two number




No comments:

Post a Comment