Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Official (Closed) - Non Sensitive

PS11-12 Hoh Jungi

Lesson10Ex1

Filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson10Ex1
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is the 3 times table printed using a while loop:");
int i = 1;
while (i < 13)
{
Console.Write("{0} ", 3 * i);
i++;
}

Console.WriteLine();
Console.WriteLine();
Console.WriteLine("This is the 4 times table printed using a for loop:");
for (i = 1; i < 13; i++)
{
Console.Write("{0} ", 4 * i);
}

Console.ReadKey();
}
}
}

LastSaved 20230613T0119 page 1 of 8 (Leave the footer alone!)


Official (Closed) - Non Sensitive
PS11-12 Hoh Jungi

Lesson10Ex2

Filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson10Eg2
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is the 12 by 12 times table:");
for (int m = 1; m < 13; m++)
{
for (int n = 1; n < 13; n++)
{
Console.Write("{0,3} ", m*n);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}

LastSaved 20230613T0119 page 2 of 8 (Leave the footer alone!)


Official (Closed) - Non Sensitive
PS11-12 Hoh Jungi

Lesson10Ex3

Filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson10Ex3
{
internal class Program
{
static void Main(string[] args)
{
float[] temperatures = { 36.9f, 37.5f, 37.8f, 37.0f, 37.9f, 37.4f, 37.1f };

Console.WriteLine("There are 7 elements in the temperatures array: ");

foreach (float temp in temperatures)


{
Console.Write("{0} ", temp);
}

Console.WriteLine();
Console.WriteLine();
Console.WriteLine("The second element is {0}", temperatures[1]);
Console.WriteLine("The fifth element is {0}", temperatures[4]);
Console.WriteLine("The last element is {0}", temperatures[6]);

LastSaved 20230613T0119 page 3 of 8 (Leave the footer alone!)


Official (Closed) - Non Sensitive
PS11-12 Hoh Jungi

while (true)
{
Console.WriteLine();
Console.Write("Enter the temperature to find [0 to Exit]: ");
float tempToFind = float.Parse(Console.ReadLine());
if (tempToFind == 0)
{
Console.WriteLine("Bye");
break;
}

else
{
bool found = false;
foreach (float temp in temperatures)
{
if (temp == tempToFind)
{
found = true;
break;
}
}

if (found)
Console.Write("{0} found", tempToFind);

else
Console.Write("{0} not found", tempToFind);

Console.WriteLine();
}
}
Console.ReadKey();
}
}
}

LastSaved 20230613T0119 page 4 of 8 (Leave the footer alone!)


Official (Closed) - Non Sensitive
PS11-12 Hoh Jungi

Lesson10Ex4

Filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson10Ex4
{
internal class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of weights you want to average: ");
int nWeights = int.Parse(Console.ReadLine());
float[] weights = new float[nWeights];

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


{
Console.Write("Enter weights[{0}] (in kg): ", i);
weights[i] = float.Parse(Console.ReadLine());
}

Console.WriteLine();
Console.WriteLine("You have entered 5 weights (in kg):");
float totalweight = 0;
foreach (float weight in weights)
{
Console.Write("{0} ", weight);
totalweight = totalweight + weight;
}

LastSaved 20230613T0119 page 5 of 8 (Leave the footer alone!)


Official (Closed) - Non Sensitive
PS11-12 Hoh Jungi

Console.WriteLine();
Console.WriteLine();
float averweight = totalweight / weights.Length;
Console.WriteLine("The average weight is {0} kg", averweight);
Console.ReadKey();
}
}
}

LastSaved 20230613T0119 page 6 of 8 (Leave the footer alone!)


Official (Closed) - Non Sensitive
PS11-12 Hoh Jungi

Lesson10Ex5

Filename: Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson10Ex5
{
class Fruit
{
public string Name;
public float Price;

public Fruit(string name, float price)


{
Name = name;
Price = price;
}
}
internal class Program

LastSaved 20230613T0119 page 7 of 8 (Leave the footer alone!)


Official (Closed) - Non Sensitive
PS11-12 Hoh Jungi

{
static void Main(string[] args)
{
Fruit[] fruits = new Fruit[7];
string[] name = { "apples", "oranges", "pears", "grapes", "strawberries", "blueberries", "avocadoes" };
float[] price = { 4.50f, 5.50f, 3.90f, 5.60f, 7.80f, 9.90f, 12.90f };

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


{
fruits[i] = new Fruit(name[i], price[i]);
}

Console.WriteLine("These are the fruit in the store:");


foreach (Fruit fruit in fruits)
{
Console.WriteLine("{0}: ${1:F2}", fruit.Name, fruit.Price);
}

Console.WriteLine();
Console.Write("Enter the fruits to find: ");
string fruitFind = Console.ReadLine();
bool found = false;
foreach (Fruit fruit in fruits)
{
if (fruit.Name == fruitFind)
{
Console.WriteLine("The price of {0} is ${1:F2}", fruitFind, fruit.Price);
found = true;
break;
}
}

if (!found)
Console.WriteLine("Sorry, {0} not found", fruitFind);

Console.ReadKey();
}
}
}

LastSaved 20230613T0119 page 8 of 8 (Leave the footer alone!)

You might also like