Professional Documents
Culture Documents
Lesson10 - Exercises (PS11-12 Hoh Jungi)
Lesson10 - Exercises (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();
}
}
}
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();
}
}
}
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();
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]);
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();
}
}
}
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];
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;
}
Console.WriteLine();
Console.WriteLine();
float averweight = totalweight / weights.Length;
Console.WriteLine("The average weight is {0} kg", averweight);
Console.ReadKey();
}
}
}
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;
{
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 };
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();
}
}
}