One-D Array

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

One-D Array

score

LASQUETY, JOHN DENVER S. MR. GENE JUSTINE P. ROSALES


Name of Student Name of Professor

11-18-19 11-19-19
Date Performed Date Submitted
LASQUETY, JOHN DENVER S.
BSIT 102

A. ONE DIMENSIONAL ARRAY – Number Array

PROBLEM #01
Create a program that will ask the user to enter 10 numbers and display it in ascending
order.

Code: int[] x = new int[10];


Console.WriteLine("Enter 10 numbers: ");
{
for (int i = 0; i < x.Length; i++)
{
Console.Write("");
x[i] = int.Parse(Console.ReadLine());
}

Console.WriteLine("");

Array.Sort(x);

Console.WriteLine("Element value of array in ascending order: ");

for (int i = 0; i < x.Length; i++)


{
Console.Write(x[i] + " ");
}
Console.ReadLine();
}
PROBLEM #02
Create a program that will convert a decimal number (positive value) to its equivalent
binary number.
 Use array for binary values
Code:
int input, b;

int[] a = new int[10];

Console.Write("Enter a decimal number: ");


input = int.Parse(Console.ReadLine());

for (b = 0; input > 0; b++)


{
a[b] = input % 2;
input = input / 2;
}
Console.Write("Binary equivalent: ");
for (b = b - 1; b >= 0; b--)
{
Console.Write(a[b]);
}
Console.Read();
PROBLEM #03
Create a program that will ask the user to enter 10 numbers and display the 1st and 2nd to
highest number and 1st and 2nd to the lowest number.
 Don’t use array sorting
Code: int[] a = new int[10];
int q, w;

Console.WriteLine("Enter ten numbers: ");


for (int b = 0; b < a.Length; b++)
{

a[b] = int.Parse(Console.ReadLine());
}

w = a.Min();
q = a.Max();

foreach (int b in a)
{
if (b > w && b < q)
{
w = b;
}

}
Console.WriteLine();
Console.WriteLine("First to the highest: {0}", q);
Console.WriteLine("Second to the highest: {0}", w);

int e, r;

r = a.Max();
e = a.Min();

foreach (int b in a)
{
if (b < r && b > e)
{
r = b;
}
}

Console.WriteLine("First to the lowest: {0}", e);


Console.WriteLine("Second to the lowest: {0}", r);

Console.ReadLine();
Output of problem #3:
PROBLEM #04
Create a program that will check if a given word is a palindrome or not a palindrome.
Code:
string x, r;
Console.Write("Enter a word: ");
x = Console.ReadLine();

char[] c = x.ToCharArray();

Array.Reverse(c);
r = new string(c);

bool y = x.Equals(r, StringComparison.OrdinalIgnoreCase);


if (y == true)
{
Console.WriteLine(x + " is a Palindrome");
}
else
{
Console.WriteLine(x + " is not a Palindrome");
}
Console.Read();

You might also like