Add Lesson 1 Task 3
This commit is contained in:
parent
d7c3bee155
commit
804920c171
2 changed files with 52 additions and 0 deletions
42
Lesson_1/Task_3/Program.cs
Normal file
42
Lesson_1/Task_3/Program.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Lesson 1 Task 3: get a list of ten numbers and duplicate a number entered by user
|
||||
* Author: Stanislav Mykhailenko
|
||||
* License: Unlicense
|
||||
*/
|
||||
|
||||
int duplicate;
|
||||
List<int> numbers = new List<int>();
|
||||
string? userInput;
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
int newNumber;
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write(string.Format("Enter number {0}: ", i + 1));
|
||||
userInput = Console.ReadLine();
|
||||
} while (!int.TryParse(userInput, out newNumber));
|
||||
|
||||
numbers.Add(newNumber);
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
Console.Write("Enter a number to duplicate: ");
|
||||
userInput = Console.ReadLine();
|
||||
} while (!int.TryParse(userInput, out duplicate));
|
||||
|
||||
for (int i = 0; i < numbers.Count; i++) {
|
||||
if (numbers[i] == duplicate)
|
||||
{
|
||||
numbers.Insert(i, duplicate);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(
|
||||
string.Join(", ",
|
||||
numbers.Select(number => number.ToString())
|
||||
)
|
||||
);
|
10
Lesson_1/Task_3/Task_3.csproj
Normal file
10
Lesson_1/Task_3/Task_3.csproj
Normal file
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
Reference in a new issue