-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
131 lines (105 loc) · 4.37 KB
/
Program.cs
File metadata and controls
131 lines (105 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Задача 25: Напишите цикл, который принимает на вход два числа (A и B) и возводит число A в натуральную степень B.
// 3, 5 -> 243 (3⁵)
// 2, 4 -> 16
Console.WriteLine($"\nЗадача 25. Возведене числа A в натуральную степень B");
int Exponentiation(int numberA, int numberB){
int result = 1;
for(int i=1; i <= numberB; i++){
result = result * numberA;
}
// int result = Math.Pow(numberA, numberB);
return result;
}
Console.Write("Введите число A: ");
int numberA = Convert.ToInt32(Console.ReadLine());
Console.Write("Введите число B: ");
int numberB = Convert.ToInt32(Console.ReadLine());
int exponentiation = Exponentiation(numberA, numberB);
Console.WriteLine("Ответ: " + exponentiation);
// Задача 27: Напишите программу, которая принимает на вход число и выдаёт сумму цифр в числе.
// 452 -> 11
// 82 -> 10
// 9012 -> 12
Console.WriteLine($"\nЗадача 27. Выдаёт сумму цифр в числе");
Console.Write("Введите число N: ");
int numberN = Convert.ToInt32(Console.ReadLine());
int SumNumber(int numberN){
int counter = Convert.ToString(numberN).Length;
int advance = 0;
int result = 0;
for (int i = 0; i < counter; i++){
advance = numberN - numberN % 10;
result = result + (numberN - advance);
numberN = numberN / 10;
}
return result;
}
int sumNumber = SumNumber(numberN);
Console.WriteLine("Сумма цифр в числе: " + sumNumber);
// Задача 29: Напишите программу, которая задаёт массив из 8 элементов и выводит их на экран.
// 1, 2, 5, 7, 19 -> [1, 2, 5, 7, 19]
// 6, 1, 33 -> [6, 1, 33]
Console.WriteLine($"\nЗадача 29. Ряд чисел преобразует в массив");
Console.Write("Введите ряд чисел, разделенных запятой : ");
string? seriesOfNumbers = Console.ReadLine();
seriesOfNumbers = seriesOfNumbers + ","; // дополнительня запятая для обозначения конца строки
// функция удаления пробелов из строки
string RemovingSpaces (string series){
string seriesNew = "";
for (int i = 0; i < series.Length; i++)
{
if (series[i] != ' ')
{
seriesNew += series[i];
}
}
return seriesNew;
}
// функция проверки на правильность ввода
void СheckNumber2 (int series){
if (series == '0'||series == '1'||series == '2'
||series == '3'||series == '4'||series == '5'||series == '6'
||series == '7'||series == '8'||series == '9'||series == ','
||series == '-')
{
}
else {
Console.WriteLine($"Ошибка ввода символа. Вводи цифры.");
}
}
// функция создания и заполнения массива из строки
int[] ArrayOfNumbers(string seriesNew){
int[] arrayOfNumbers = new int[1]; // инициализация массива из 1 элемента
int j =0;
for (int i = 0; i < seriesNew.Length; i++){
string seriesNew1 = "";
while (seriesNew[i] != ',' && i < seriesNew.Length){
seriesNew1 += seriesNew[i];
СheckNumber2(seriesNew[i]);
i++;
}
arrayOfNumbers[j] = Convert.ToInt32(seriesNew1); // заполняет массив значениями из строки
if (i < seriesNew.Length-1){
arrayOfNumbers = arrayOfNumbers.Concat(new int[] {0}).ToArray(); // добавляет новый нулевой элемент в конец массива
}
j++;
}
return arrayOfNumbers;
}
// функция вывода массива на печать
void PrintArry(int[] coll){
int count = coll.Length;
int index = 0;
Console.Write("[");
while(index < count){
Console.Write(coll[index]);
index++;
if (index < count){
Console.Write(", ");
}
}
Console.Write("]");
}
string seriesNew = RemovingSpaces(seriesOfNumbers);
int[] arrayOfNumbers = ArrayOfNumbers(seriesNew);
PrintArry(arrayOfNumbers);