C# program for swapping of two arrays

Here, we are going to learn how to swap two arrays in C#.Net?
Submitted by Nidhi, on April 27, 2021

In the below program, we will learn how to swap two integer arrays by each other?

Program:

The source code to swap of two arrays is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare to manage index of arrays
            int index = 0;

            //Temporary variable two swap arrays
            int temp = 0;

            //Declare array that contains 5 integer elements
            int[] arr1 = new int[5];
            int[] arr2 = new int[5];

            //Now read values for first array elements.
            Console.WriteLine("Enter value of array1 elements...");
            for (index = 0; index < arr1.Length; index++)
            {
                Console.Write("Element arr1[" + (index + 1) + "]: ");
                arr1[index] = int.Parse(Console.ReadLine());
            }

            //Now read values for second array elements.
            Console.WriteLine("Enter value of array2 elements...");
            for (index = 0; index < arr2.Length; index++)
            {
                Console.Write("Element arr2[" + (index + 1) + "]: ");
                arr2[index] = int.Parse(Console.ReadLine());
            }

            Console.WriteLine("\n\nBefore Swapping...");
            Console.WriteLine("Array 1 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr1[index] + " ");
            }
            Console.WriteLine("\nArray 2 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr2[index] + " ");
            }

            Console.WriteLine();
            Console.WriteLine();

            //Now we swap two arrays
            for (index = 0; index < 5; index++)
            {
                temp        = arr1[index];
                arr1[index] = arr2[index];
                arr2[index] = temp;

            }
            
            Console.WriteLine("\n\nAfter Swapping...");
            Console.WriteLine("Array 1 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr1[index]+" ");
            }
            Console.WriteLine("\nArray 2 Elements\n");
            for (index = 0; index < 5; index++)
            {
                Console.Write(arr2[index] + " ");
            }
            
            Console.WriteLine();
        }
    }
}

Output:

Enter value of array1 elements...
Element arr1[1]: 10
Element arr1[2]: 20
Element arr1[3]: 30
Element arr1[4]: 40
Element arr1[5]: 50
Enter value of array2 elements...
Element arr2[1]: 11
Element arr2[2]: 12
Element arr2[3]: 13
Element arr2[4]: 14
Element arr2[5]: 15


Before Swapping...
Array 1 Elements

10 20 30 40 50
Array 2 Elements

11 12 13 14 15     



After Swapping...
Array 1 Elements   

11 12 13 14 15
Array 2 Elements

10 20 30 40 50              

C# Basic Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.