From 804920c171683b44575e29cdfdac3ea6f3158c44 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Mon, 6 Mar 2023 14:18:50 +0200 Subject: [PATCH] Add Lesson 1 Task 3 --- Lesson_1/Task_3/Program.cs | 42 +++++++++++++++++++++++++++++++++++ Lesson_1/Task_3/Task_3.csproj | 10 +++++++++ 2 files changed, 52 insertions(+) create mode 100644 Lesson_1/Task_3/Program.cs create mode 100644 Lesson_1/Task_3/Task_3.csproj diff --git a/Lesson_1/Task_3/Program.cs b/Lesson_1/Task_3/Program.cs new file mode 100644 index 0000000..6105750 --- /dev/null +++ b/Lesson_1/Task_3/Program.cs @@ -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 numbers = new List(); +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()) + ) +); diff --git a/Lesson_1/Task_3/Task_3.csproj b/Lesson_1/Task_3/Task_3.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/Lesson_1/Task_3/Task_3.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + +