0% found this document useful (0 votes)
39 views

ascending The Elements of The Array: Console Console

The document shows code to calculate the sum of elements in an array and then sort the elements of an array in ascending order by swapping adjacent elements.

Uploaded by

menrie luz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

ascending The Elements of The Array: Console Console

The document shows code to calculate the sum of elements in an array and then sort the elements of an array in ascending order by swapping adjacent elements.

Uploaded by

menrie luz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

static void Main(string[] args)

{
//Sum of all elements in an array
int[] array=new int []{2,2,2,2};
int sum1 = array.Sum();
Console.WriteLine("The sum is: {0}", sum1);
Console.ReadLine();
}

//Ascending the elements of the array


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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[5];
int tmp, j;
Console.WriteLine("Enter the element of the array:");
for (int i = 0; i < a.Length; i++)
{
Console.Write("Element {0};",i);
a[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < a.Length; i++)
{
for ( j = i + 1; j < a.Length; j++)
{
if (a[j] < a[i])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
Console.WriteLine("{0} {1} {2} {3} {4}", a[0],
a[1],a[2],a[3],a[4]);
Console.ReadLine();
}
}
}

You might also like