Basic Programming C# Examples

C# Program to Swap two Numbers

The Program in C# Program to Swap two Numbers is given below:

using System;

namespace SwapTwoNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            
            Console.WriteLine("Hello Codeauri Famuly, Enter the first number and Second number here to Swap their numbers: ");
            Console.WriteLine("Well,enter the first number: ");
            int num1 = Convert.ToInt32(Console.ReadLine());
            
            Console.WriteLine("Similarly,enter the second number: ");
            int num2 = Convert.ToInt32(Console.ReadLine());
            
            Console.WriteLine("This is before swapping: ");
            Console.WriteLine("num1: " + num1 + " num2: " + num2);
            
            int temp = num1;
            num1 = num2;
            num2 = temp;
            
            Console.WriteLine("This is after swapping: ");
            Console.WriteLine("num1: " + num1 + " num2: " + num2);
        }
    }
}

Output:

Hello Codeauri Famuly, Enter the first number and Second number here to find their sum:
Well, enter the first number:
20550226
Similarly,enter the second number:
20560423
Okay, the sum of 20550226 and 20560423 is: 41110649

Pro-Tips💡

Here are the step by step execution of the above program:

  1. The code starts by importing the System namespace.
  2. The namespace SwapTwoNumbers is created.
  3. A class named Program is created within the namespace.
  4. A Main method is created within the Program class.
  5. The program starts by displaying a message to the user to enter two numbers.
  6. The first number entered by the user is stored in the num1 variable after converting it to an integer.
  7. The second number entered by the user is stored in the num2 variable after converting it to an integer.
  8. The original values of num1 and num2 are displayed.
  9. A temporary variable temp is created and is assigned the value of num1.
  10. The value of num1 is then updated with the value of num2.
  11. The value of num2 is then updated with the value of temp.
  12. Finally, the values of num1 and num2 are displayed after swapping.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X