This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2023_Stanislav_Mykhailenko/Lesson_1/Task_3/Program.cs

40 lines
767 B
C#
Raw Normal View History

2023-03-06 12:18:50 +00:00
/*
* 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>();
for (int i = 0; i < 10; i++)
{
2023-03-19 12:03:57 +00:00
int newNumber;
2023-03-06 12:18:50 +00:00
2023-03-19 12:03:57 +00:00
while (true)
{
Console.Write($"Enter number {i + 1}: ");
if (int.TryParse(Console.ReadLine(), out newNumber))
break;
}
2023-03-06 12:18:50 +00:00
2023-03-19 12:03:57 +00:00
numbers.Add(newNumber);
2023-03-06 12:18:50 +00:00
}
2023-03-19 12:03:57 +00:00
while (true)
2023-03-06 12:18:50 +00:00
{
2023-03-19 12:20:52 +00:00
Console.Write("Enter number to duplicate: ");
2023-03-19 12:03:57 +00:00
if (int.TryParse(Console.ReadLine(), out duplicate))
break;
}
2023-03-06 12:18:50 +00:00
for (int i = 0; i < numbers.Count; i++) {
2023-03-19 12:03:57 +00:00
if (numbers[i] == duplicate)
{
numbers.Insert(i, duplicate);
i++;
}
2023-03-06 12:18:50 +00:00
}
2023-03-18 22:08:23 +00:00
Console.WriteLine(String.Join(", ", numbers));