תרגיל 1.2.1: חיבור שני מספרים
כתבו תכנית שקוראת שני מספרים מהמשתמש ומדפיסה את סכומם.
פתרון
1
2
3
4
5
6
7
8
9
Console.Write("Enter first number: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Enter second number: ");
int b = int.Parse(Console.ReadLine());
int sum = a + b;
Console.WriteLine("Sum is: " + sum);
תרגיל 1.2.2: ממוצע של שלושה ציונים
כתבו תכנית שקוראת שלושה ציונים ומחשבת את ממוצעם (עם נקודה עשרונית).
פתרון
1
2
3
4
5
6
7
8
9
10
11
12
Console.Write("Enter grade 1: ");
double g1 = double.Parse(Console.ReadLine());
Console.Write("Enter grade 2: ");
double g2 = double.Parse(Console.ReadLine());
Console.Write("Enter grade 3: ");
double g3 = double.Parse(Console.ReadLine());
double average = (g1 + g2 + g3) / 3;
Console.WriteLine("Average: " + average);
תרגיל 1.2.3: שארית החלוקה
כתבו תכנית שקוראת מספר ומדפיסה את השארית שלו בחלוקה ל־3.
פתרון
1
2
3
4
5
6
Console.Write("Enter a number: ");
int num = int.Parse(Console.ReadLine());
int remainder = num % 3;
Console.WriteLine("Remainder when divided by 3: " + remainder);
תרגיל 1.2.4: אחוז מתוך מספר
כתבו תכנית שקוראת מספר ואחוז (למשל 20) ומדפיסה כמה זה מתוך המספר.
פתרון
1
2
3
4
5
6
7
8
9
Console.Write("Enter number: ");
double num = double.Parse(Console.ReadLine());
Console.Write("Enter percentage: ");
double percent = double.Parse(Console.ReadLine());
double result = num * percent / 100;
Console.WriteLine("Result: " + result);
תרגיל 1.2.5: שטח מלבן
כתבו תכנית שקוראת את האורך והרוחב של מלבן ומדפיסה את שטחו.
פתרון
1
2
3
4
5
6
7
8
9
Console.Write("Enter length: ");
double length = double.Parse(Console.ReadLine());
Console.Write("Enter width: ");
double width = double.Parse(Console.ReadLine());
double area = length * width;
Console.WriteLine("Area of rectangle: " + area);
תרגיל 1.2.6: הצגת שם ותו מהשם
כתבו תכנית שקוראת שם פרטי ומדפיסה את התו הראשון בשם.
פתרון
1
2
3
4
5
6
Console.Write("Enter your name: ");
string name = Console.ReadLine();
char firstChar = name[0]; // name פניה לתו הראשון במחרוזת
Console.WriteLine("First letter: " + firstChar);
תרגיל 1.2.7: כפל וחיבור
כתבו תכנית שקוראת שני מספרים ומדפיסה גם את המכפלה וגם את הסכום שלהם.
פתרון
1
2
3
4
5
6
7
8
Console.Write("Enter number 1: ");
int x = int.Parse(Console.ReadLine());
Console.Write("Enter number 2: ");
int y = int.Parse(Console.ReadLine());
Console.WriteLine("Sum: " + (x + y));
Console.WriteLine("Product: " + (x * y));
תרגיל 1.2.8: סכום ספרות של מספר בן שתי ספרות
כתבו תכנית שקוראת מספר בן שתי ספרות ומדפיסה את סכום הספרות.
פתרון
1
2
3
4
5
6
7
8
9
Console.Write("Enter two-digit number: ");
int num = int.Parse(Console.ReadLine());
int tens = num / 10;
int ones = num % 10;
int sum = tens + ones;
Console.WriteLine("Sum of digits: " + sum);
תרגיל 1.2.9: המרה משקלים לדולרים
כתבו תכנית שקוראת סכום בשקלים ואת שער הדולר, ומדפיסה את הסכום בדולרים.
פתרון
1
2
3
4
5
6
7
8
9
Console.Write("Enter amount in shekels: ");
double ils = double.Parse(Console.ReadLine());
Console.Write("Enter exchange rate: ");
double rate = double.Parse(Console.ReadLine());
double usd = ils / rate;
Console.WriteLine("Amount in dollars: " + usd);
תרגיל 1.2.10: שם וגיל
כתבו תכנית שקוראת שם וגיל ומדפיסה משפט כמו:
Sara is 17 y.o.
פתרון
1
2
3
4
5
6
7
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());
Console.WriteLine(name + " is " + age + " y.o.");
מוכן.ה לאתגר?
אם סיימת לתרגל תרגילים אלו בהצלחה, זה הזמן ליישם את הידע שלך בתרגול מותאם.