You are on page 1of 6

...\pc\source\repos\ConsoleApp41\ConsoleApp41\Program.

cs 1
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace ConsoleApp41
8 {
9 internal class Program
10 {
11 static void Main(string[] args)
12 {
13 //1st exercise//
14 int sum = 0;
15 Console.WriteLine("enter a number: ");
16 int number = int.Parse(Console.ReadLine());
17 for (int i = 2; i <= number; i++)
18 {
19 if (i % 2 == 0)
20 {
21 Console.Write(i + "\t");
22 sum = sum + i;
23 }
24 }
25 Console.WriteLine();
26 Console.WriteLine("the sum of all even numbers is: " + sum);
27 Console.WriteLine();
28
29 //2nd exercise//
30 Random rnd = new Random();
31 int num, counter = 0;
32 int units, tens, hundreds;
33 Console.WriteLine("numbers that at least two of their digits
are equal:");
34 for (int i = 1; i <= 50; i++)
35 {
36 num = rnd.Next(100, 1000);
37 units = num % 10;
38 tens = (num / 10) % 10;
39 hundreds = (num / 100) % 10;
40 if ((units == tens) || (units == hundreds) || (tens ==
hundreds))
41 {
42 Console.WriteLine(num + "\t");
43 counter++;
44 }
45 }
46 Console.WriteLine("the amount of such numbers is: " +counter);
47 Console.WriteLine();
...\pc\source\repos\ConsoleApp41\ConsoleApp41\Program.cs 2
48
49 //3rd exercise//
50 int n;
51 int columnSum = 0;
52 Console.WriteLine("enter number: ");
53 n = int.Parse(Console.ReadLine());
54 for (int i = 1; i <= n; i++)
55 {
56 Console.Write(i+"+");
57 columnSum = columnSum + i;
58 }
59 Console.WriteLine("\b=" +columnSum);
60 Console.WriteLine();
61
62 //4th exercise//
63 int numSum;
64 int counterSum = 0;
65 int units1, tens1, hundreds1;
66 for (int i = 100; i <= 999; i++)
67 {
68 units1 = i % 10;
69 tens1 = (i / 10) % 10;
70 hundreds1 = (i / 100) % 10;
71 numSum = units1 + tens1 + hundreds1;
72 if (i % numSum == 0)
73 {
74 counterSum++;
75 Console.Write(i + " ");
76 }
77 }
78 Console.WriteLine("counterSum = " +counterSum);
79 Console.WriteLine();
80
81 //5th exercise//
82 int positiveNumber;
83 // ‫המונה יספור את כמות המחלקים של המספר‬
84 int counterNum = 0;
85 Console.WriteLine("enter a positive number: ");
86 positiveNumber = int.Parse(Console.ReadLine());
87
88 // ‫בדיקה אם מדובר במספר ראשוני‬
89 for (int i = 1; i <= positiveNumber; i++)
90 {
91 if (positiveNumber % i == 0)
92 {
93 counterNum++;
94 }
95 }
96 Console.WriteLine("counter = " +counterNum);
...\pc\source\repos\ConsoleApp41\ConsoleApp41\Program.cs 3
97 if ((counterNum == 1) || (counterNum == 2))
98 {
99 Console.WriteLine("prime number!");
100 }
101 Console.WriteLine();
102
103 //6th exercise//
104 Console.WriteLine("type a number: ");
105 int n = int.Parse(Console.ReadLine());
106 Random rnd = new Random();
107 int number;
108 double avg;
109 int counterEight = 0, counterNine = 0, counterTen = 0;
110 for (int i = 1; i <= n; i++)
111 {
112 number = rnd.Next(8, 11);
113 switch (number)
114 {
115 case 8:
116 {
117 counterEight++;
118 break;
119 }
120 case 9:
121 {
122 counterNine++;
123 break;
124 }
125 case 10:
126 {
127 counterTen++;
128 break;
129 }
130 }
131 }
132 Console.WriteLine("8=" + counterEight);
133 Console.WriteLine("9=" + counterNine);
134 Console.WriteLine("10=" + counterTen);
135 if ((counterEight >= counterNine) && (counterEight >=
counterTen))
136 {
137 Console.WriteLine("the number that was randomized the most
is: " +8);
138 }
139 if ((counterNine >= counterEight) && (counterNine >=
counterTen))
140 {
141 Console.WriteLine("the number that was randomized the most
is: " +9);
...\pc\source\repos\ConsoleApp41\ConsoleApp41\Program.cs 4
142 }
143 if ((counterTen >= counterNine) && (counterTen >=
counterEight))
144 {
145 Console.WriteLine("the number that was randomized the most
is: " +10);
146 }
147 avg = (counterEight * 8) + (counterNine * 9) + (counterTen *
10);
148 avg = avg / (counterEight + counterNine + counterTen);
149 Console.WriteLine("avg=" +avg);
150 if (counterTen > 0)
151 {
152 Console.WriteLine("the biggest number is: 10");
153 }
154 else
155 if (counterNine > 0)
156 {
157 Console.WriteLine("the biggest number is: 9");
158 }
159 else
160 {
161 Console.WriteLine("the biggest number is: 8");
162 }
163
164 //7th exercise//
165 int num1, num2;
166 int sumNum = 0;
167 // ‫קליטה של נתונים בפעם הראשונה‬
168 Console.WriteLine("type two numbers: ");
169 num1 = int.Parse(Console.ReadLine());
170 num2 = int.Parse(Console.ReadLine());
171 // (‫בדיקה אם הנתונים תקינים )עומדים בהגדרות השונות‬
172 while (num1 * num2 != 0)
173 {
174 // ‫עיבוד נתונים‬
175 Console.WriteLine("the multiplication of the two numbers
is: " +(num1 * num2));
176 sumNum = sumNum + (num1 * num2);
177
178 // ‫קליטה חוזרת של נתונים לקראת הביסוס הבא‬
179 Console.WriteLine("type two numbers: ");
180 num1 = int.Parse(Console.ReadLine());
181 num2 = int.Parse(Console.ReadLine());
182 }
183 Console.WriteLine("the sum is = " +sumNum);
184 if (sumNum > 0)
185 {
186 Console.WriteLine("the sum is positive!");
...\pc\source\repos\ConsoleApp41\ConsoleApp41\Program.cs 5
187 }
188 else
189 {
190 Console.WriteLine("the sum is negative!");
191 }
192
193 //8th exercise//
194 int notebooks, sumNotebooks = 0, sumPrice = 0;
195
196 Console.Write("the price of one notebook is: ");
197 int price = int.Parse(Console.ReadLine());
198 Console.WriteLine();
199
200 for (int students = 1; students <= 10; students++)
201 {
202 Console.Write("the amount of notebooks per student: ");
203 notebooks = int.Parse(Console.ReadLine());
204 sumNotebooks += notebooks;
205 }
206 Console.WriteLine();
207 Console.WriteLine("the total amount of notebooks bought is: "
+sumNotebooks);
208 sumPrice = (sumNotebooks * price);
209 Console.WriteLine("the total price for all the notebooks
bought is: " +sumPrice);
210
211 //9th exercise//
212 int thousands, hundreds, tens, units;
213 int counter = 0;
214 for (int i = 1000; i <= 9999; i++)
215 {
216 thousands = i / 1000;
217 hundreds = (i / 100) % 10;
218 tens = (i % 100) / 10;
219 units = (i % 10);
220 if ((thousands == tens) && (hundreds== units))
221 {
222 Console.Write(i+" ");
223 counter++;
224 }
225 }
226 Console.WriteLine();
227 Console.WriteLine("there are "+counter+" numbers like
these.");
228
229 //10th exercise//
230 int counter = 0;
231 int number;
232 for (int i = 0; i < 10; i++)
...\pc\source\repos\ConsoleApp41\ConsoleApp41\Program.cs 6
233 {
234 Console.Write("enter number--> ");
235 number=int.Parse(Console.ReadLine());
236 if (number > 4)
237 {
238 counter++;
239 }
240 }
241 // ‫מציגים למשתמש את ערך המשתנה‬
242 Console.WriteLine("counter="+ counter);
243 }
244 }
245 }
246

You might also like