תרגילים ב-#C: תרגילים בסיסיים (א’)
הדפסה בשורות נפרדות
כיתבו תוכנית שתדפיס את המילה Hello ואת השם שלכם בשתי שורות נפרדות.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { Console.WriteLine("Hello"); Console.WriteLine("Eyal Shaked"); } } }
סכום מספרים
כיתבו תוכנית שתדפיס את הסכום של שני המספרים השלמים: 33 ו- 55.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { int num1 = 55, num2 = 33, sum; sum = num1 + num2; Console.WriteLine("The sum is: " + sum); } } }
קלט מספר שלם
כיתבו תוכנית שתקלוט מהמשתמש שני מספרים שלמים ותדפיס את סכומם.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { int num1, num2, sum; Console.Write("Enter first number: "); num1 = int.Parse(Console.ReadLine()); Console.Write("Enter second number: "); num2 = int.Parse(Console.ReadLine()); sum = num1 + num2; Console.WriteLine("The sum is: " + sum); } } }
קלט של תו (char)
כיתבו תוכנית שתקלוט מהמשתמש 3 אותיות מטיפוס char ותדפיס את האותיות בסדר הפוך.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { char ch1, ch2, ch3; Console.Write("Enter first character: "); ch1 = char.Parse(Console.ReadLine()); Console.Write("Enter second character: "); ch2 = char.Parse(Console.ReadLine()); Console.Write("Enter third character: "); ch3 = char.Parse(Console.ReadLine()); Console.WriteLine("The characters in reverse order: {0} {1} {2}",ch3,ch2,ch1); } } }
קלט שלם, תוצאת חלוקה שלמה
כיתבו תוכנית שתקלוט מהמשתמש שני מספרים שלמים x,y ותדפיס את תוצאת החלוקה השלמה ביניהם x/y.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { int x, y, result; Console.Write("Enter first number: "); x = int.Parse(Console.ReadLine()); Console.Write("Enter second number: "); y = int.Parse(Console.ReadLine()); result = x / y; Console.WriteLine("The result is: " + result); } } }
קלט שלם, תוצאת חלוקה ממשית
כיתבו תוכנית שתקלוט מהמשתמש שני מספרים שלמים x,y ותדפיס את תוצאת החלוקה ביניהם x/y בצורה לא שלמה (שבר עשרוני).
using System; namespace BasicExercises { class Program { static void Main(string[] args) { int x, y; double result; Console.Write("Enter first number: "); x = int.Parse(Console.ReadLine()); Console.Write("Enter second number: "); y = int.Parse(Console.ReadLine()); result = (double) x / y; Console.WriteLine("The result is: " + result); } } }
קלט ממשי, תוצאת חלוקה ממשית
כיתבו תוכנית שתקלוט מהמשתמש שני מספרים ממשיים x,y ותדפיס את תוצאת החלוקה ביניהם x/y בצורה לא שלמה (שבר עשרוני).
using System; namespace BasicExercises { class Program { static void Main(string[] args) { double x, y, result; Console.Write("Enter first number: "); x = int.Parse(Console.ReadLine()); Console.Write("Enter second number: "); y = int.Parse(Console.ReadLine()); result = x / y; Console.WriteLine("The result is: " + result); } } }
החלפה בין ערכים (Swap)
כיתבו תוכנית המחליפה בין ערכיהם של שני מספרים שלמים. התכנית תדפיס את ערכי המשתנים לפני ההחלפה ואחרי ההחלפה.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { int a=5, b=1, temp; Console.WriteLine("Values before swapping: {0} {1}", a, b); temp = a; a = b; b = temp; Console.WriteLine("Values after swapping: {0} {1}", a, b); } } }
הכפלת מספרים
כיתבו תוכנית שתקלוט מהמשתמש שלושה מספרים ממשיים x,y,z ותדפיס את תוצאת ההכפלה ביניהם x*y*z.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { double x, y, z, result; Console.WriteLine("Enter first number: "); x = double.Parse(Console.ReadLine()); Console.WriteLine("Enter second number: "); y = double.Parse(Console.ReadLine()); Console.WriteLine("Enter third number: "); z = double.Parse(Console.ReadLine()); result = x * y * z; Console.WriteLine("The result is: " + result); } } }
טבלת הכפלות של מספר
כיתבו תוכנית שתקלוט מהמשתמש מספר שלם ותדפיס את טבלת ההכפלות שלו מ-0 עד 10.
using System; namespace BasicExercises { class Program { static void Main(string[] args) { int num; Console.WriteLine("Enter a number: "); num = int.Parse(Console.ReadLine()); Console.WriteLine("The multiplication table of {0} is:", num); Console.WriteLine("{0} * 0 = {1}",num,num*0); Console.WriteLine("{0} * 1 = {1}", num, num * 1); Console.WriteLine("{0} * 2 = {1}", num, num * 2); Console.WriteLine("{0} * 3 = {1}", num, num * 3); Console.WriteLine("{0} * 4 = {1}", num, num * 4); Console.WriteLine("{0} * 5 = {1}", num, num * 5); Console.WriteLine("{0} * 6 = {1}", num, num * 6); Console.WriteLine("{0} * 7 = {1}", num, num * 7); Console.WriteLine("{0} * 8 = {1}", num, num * 8); Console.WriteLine("{0} * 9 = {1}", num, num * 9); Console.WriteLine("{0} * 10 = {1}", num, num * 10); } } }
Please share this article if you like it!