1.เขียน โปรแกรมหาปริมาตรของลูกบาศก์ โดยรับค่าเป็นจำนวนเต็มที่มากกว่า 10 แต่น้อยกว่า 100 เท่านั้น หากไม่อยู่ในช่วงตัวเลขนี้ไม่ต้องแสดงผลออกทางหน้าจอ
ตอบ
int x;
Console.Write("Plaese input Number : ");
x = int.Parse(Console.ReadLine());
if (x <= 100 && x >= 10)
Console.WriteLine(x);
2.สร้างตัวแปรเลขจำนวนเต็ม 2 ตัวเพื่อรับค่า x และ y แล้วแสดงผลลัพธ์จากการคำนวณนี้ x2 +2xy+y2 ทางหน้าจอ
ตอบ
int x, y;
Console.Write("Plaese input X : ");
x = int.Parse(Console.ReadLine());
Console.Write("Plaese input Y : ");
y = int.Parse(Console.ReadLine());
Console.WriteLine("\n"+"x^2 + 2xy + y^2 "+"\n");
Console.WriteLine(x+"^2"+" + 2"+"("+x+")"+"("+y+")"+" + "+y+"^2 " +" = "+((x*x)+(2*(x*y))+(y*y)));
Console.WriteLine();
3.สร้าง ตัวแปรเลขจำนวนเต็ม เพื่อใส่ปีพ.ศ.เกิด แล้วคำนวณอายุจากปีปัจจุบันนี้ แสดงผลลัพธ์อายุทางหน้าจอ เฉพาะกรณีที่อายุมากกว่า 18 เท่านั้น
ตอบ
int age,birthyear;
Console.Write("Please input BirthYear : ");
birthyear = int.Parse(Console.ReadLine());
age = 2554 - birthyear;
if (age>=18)
Console.WriteLine("Age ="+age);
4.ให้กรอกคะแนนเต็มบวกที่ไม่เกิน 100 หากคะแนน 50 ขึ้นไป ให้แสดงผลลัพธ์ทางหน้าจอว่า You pass นอกนั้นให้แสดงคำว่า You fail
ตอบ
int score;
Console.Write("Plaese input your score : ");
score = int.Parse(Console.ReadLine());
if (score <=100 && score >=50)
Console.WriteLine("You pass :)");
else
Console.WriteLine("You fail :(");
Console.WriteLine();
5.ให้สร้างตัวแปรเลขจำนวนเต็ม 3 ตัวเพื่อรับค่าตัวเลข และสร้าง แสดงค่าที่มากที่สุดใน 3 ตัวทางหน้าจอ
ตอบ
int x, y, z;
Console.Write("Please input Number 1 : ");
x = int.Parse(Console.ReadLine());
Console.Write("Please input Number 2 : ");
y = int.Parse(Console.ReadLine());
Console.Write("Please input Number 3 : ");
z = int.Parse(Console.ReadLine());
Console.WriteLine();
if (x >= y)
{
if (x>=z) Console.WriteLine("answer = "+x);
else Console.WriteLine("answer = "+z);
}
else
{
if (y>=z) Console.WriteLine("answer = "+y);
else Console.WriteLine("answer = "+z);
}
Console.WriteLine();
6.สร้างตัวแปรเลขจำนวนเต็มเพื่อรับค่า x แล้วคำนวณหา y ทาง หน้าจอ จาก
ตอบ
float x, y;
Console.Write("Please input X :");
x = int.Parse(Console.ReadLine());
Console.WriteLine();
y = 3 * x + 2;
if (x <= 10)
{
Console.WriteLine("Y = 3(x)+2 ");
Console.WriteLine(" = " + 3+"("+x+")"+"+2");
Console.Write(" = " + y);
}
else
{
y = x - 5;
Console.WriteLine("Y = x - 5");
Console.WriteLine(" = " +x+" - 5");
Console.Write(" = " + y);
}
Console.WriteLine("\n");
7.สร้างตัวแปรเลขจำนวนเต็ม เพื่อรับค่า x แล้วคำนวณหาความแตกต่างของ 2x กับ x2 แล้วแสดงผลต่างบนหน้าจอ
ตอบ
int x;
Console.Write("Please input X : ");
x = int.Parse(Console.ReadLine());
Console.WriteLine("2x = 2+"+(x)+"\n"+" = "+2*x);
Console.WriteLine();
Console.WriteLine("x^2 = "+x+"^2"+"\n"+" = "+x*x);
Console.WriteLine();
8.ให้สร้างตัวแปรเลขจำนวนเต็ม 3 ตัว แล้วรับค่าทางแป้นพิมพ์ แสดงค่าที่น้อยที่สุดใน 3 ตัวบนหน้าจอ
ตอบ
int x, y, z;
Console.Write("Please input Number1 : ");
x = int.Parse(Console.ReadLine());
Console.Write("Please input Number2 : ");
y = int.Parse(Console.ReadLine());
Console.Write("Please input Number3 : ");
z = int.Parse(Console.ReadLine());
if (x <= y)
{
if (x <= z) Console.WriteLine("answer = " + x);
else Console.WriteLine("answer = " + z);
}
else
{
if (y <=z) Console.WriteLine("answer = "+y);
else Console.WriteLine("answer = "+z);
}
9.ให้สร้างตัวแปรข้อความ เพื่อรับค่าตัวอักษร หากตัวอักษรเป็น N หรือ n ให้แสดงในว่า NO , หากเป็น Y หรือ y ให้แสดงใน Label ว่า YES และหากเป็นตัวอักษรอื่นๆ ให้แสดงว่า OTHER
ตอบ
string word;
Console.Write("Plaese input word(Y/y , N/n) : ");
word = Console.ReadLine();
if (word == "N")
Console.WriteLine("No");
else if (word == "n")
Console.WriteLine("No");
else if (word == "Y")
Console.WriteLine("Yes");
else if (word == "y")
Console.WriteLine("Yes");
else
Console.WriteLine("Other");
10.ให้สร้างตัวแปรเลขจำนวนเต็ม 2 ตัวเพื่อรับค่า x กับ y เป็นจำนวนเต็มบวก แล้วแสดงค่าผลลัพธ์จาก x2 + y2 – x – y บนหน้าจอ
ตอบ
int x, y;
Console.Write("Please input X : ");
x = int.Parse(Console.ReadLine());
Console.Write("Please input Y : ");
y = int.Parse(Console.ReadLine());
Console.WriteLine("x2 + y2 - x - y");
Console.WriteLine("= "+"("+x+")"+"2 + "+"("+y+")"+"2 - "+x+" - "+y);
Console.WriteLine("= " + "(" + x*2+")"+"+"+"(" + y*2+")"+" - " + x + " - " + y);
Console.Write("= "+(x*2+y*2-x-y));
Console.WriteLine();
11.ให้สร้างตัวแปรเลขจำนวนเต็ม 2 ตัวเพื่อรับค่า x กับ y ให้เลือกแสดงค่าผลลัพธ์ตัวที่มากกว่า ระหว่าง x2+y กับ y2+x
ตอบ
int x, y,a,b;
Console.Write("Plaese input X : ");
x = int.Parse(Console.ReadLine());
Console.Write("Plaese input Y : ");
y = int.Parse(Console.ReadLine());
a = x * x + y;
b = (y * y) + x;
Console.WriteLine("x^2+y = " + a);
Console.WriteLine("y^2+x = " + b);
Console.WriteLine();
if (a>=b)
{
Console.WriteLine("Answer: x^2+y = "+a);
}
else
{
Console.WriteLine("Answer: y^2+x = "+b);
}
Console.WriteLine();
ไม่มีความคิดเห็น:
แสดงความคิดเห็น
แสดงความเห็นเกี่ยวกับบทความตรงนี้นะ ^^