using System;
public class Program
{
public static void Main()
/*
int a = 1;
float b = 1.30f;
char c = 'A';
string d = "puwadol sirikongtham";
Console.Write("a= {0}\n",a);
Console.Write("b= {0}\n",b);
Console.Write("c= {0}\n",c);
Console.Write("d= {0}\n",d);*/
string name = "Jirapat Sae-tang";
int age = 19;
int day = 19,month = 5,year = 2544;
float gpa = 3.89f;
Console.WriteLine("NAME : {0}",name);
Console.WriteLine("AGE : {0} and Birth Day : {1}/{2}/{3}",age,day,month,year);
Console.Write("GPA : {0}",gpa);
*/
//const คือ ตัวที่ล็อกให้เป็นค่าคงที่ไม่ให้ใครมาแก้โค้ดตัวนั้นๆได้อย่างค่าพายไรงี้
/* const float PI = 3.14f;
string name = "Puwadol";
float GPA = 2.5f;
Console.WriteLine("My name is {0}",name);
Console.WriteLine("GPA:{0}",GPA);
Console.Write("PI = {0}\n",PI);
//มีตัวปัดเศษพิเศษอยูาในเลกเชอร์ ที่ใช้ในนี้คือ {0:0,0} >0,0.=ใช้อันนี้
float f1=35,f2 = 36,f3 = 38,f4 = 39,f5 = 40;
float ava = (f1+f2+f3+f4+f5)/5;
Console.WriteLine("{0}+{1}+{2}+{3}+{4} =ทศนิยม 2 ตำแหน่ง = {5:N}\n\t\t\t\t\t\t\t=เลขจำนวนเต็ม\t = {6:0,0}",f1,f2,f3,f4,f5,ava,ava);
//สูตรการหาเลขยกกำลัง
/*double x = Math.Pow(2,10);
Console.WriteLine(x);*/
Console.Write("input 1 : ");
float a = int.Parse(Console.ReadLine());
Console.Write("input 2 : ");
float b = int.Parse(Console.ReadLine());
float sum = a + b;
float sub = a - b;
float mul = a * b;
float divi= a / b ;
Console.Write("Sum = {0},Sub = {1},Multiple = {2},Divide = {3:F}",sum,sub,mul,divi);
//int year = DateTime.Now.Year; เป็นสูตรเฉพาะเอาไว้เรียกปีที่อยู่ในเครื่องคอมต้องมีตัวแปรเปิดเฉพาะ
string name;
Console.WriteLine("โปรดใส่ชื่อสมาชิกของคุณ");
name = Console.ReadLine();\n
Console.WriteLine("โปรดใส่ปี พ.ศ.เกิดของคุณ");
int y1 = int.Parse(Console.ReadLine());
int y = DateTime.Now.Year;
int yTH = y + 543;
int y2 = yTH - y1;
Console.WriteLine("ยินดีต้อนรับคุณ {0} คุณมีอายุ {1}",name,y2);
int score;
Console.WriteLine("Input your score: ");
score = int.Parse(Console.ReadLine());
Console.WriteLine("Your score is {0}\n",score);
Console.WriteLine("What you name: ");
name = Console.ReadLine();
Console.WriteLine("You name {0}\n",name);
int age,date;
float gpa;
Console.Write("What you name: ");
Console.Write("How old are you: ");
age = int.Parse(Console.ReadLine());
Console.Write("What you Birthday (daymonthyear): ");
date = int.Parse(Console.ReadLine());
Console.Write("What you GPA: ");
gpa = float.Parse(Console.ReadLine());
Console.Write("Your name is {0}\nYou {1} years old\nYour Birthday is {2}\nYou GPA :{3:F}",name,age,date,gpa);
int x = 10 - 12 / 2 + 2 * 2;
Console.WriteLine("x = {0}\n",x);
int x = 19-12/3*2+7;
Console.WriteLine("========================================");
Console.WriteLine("\t\t\t\t\t\t\t\t\tBill Coffee Shop");
Console.WriteLine("Coffee 30 Baht, Milk 15 Baht, Water 10 Baht");
Console.Write("How Many Coffee Do you want : ");
int coff = int.Parse(Console.ReadLine());
Console.Write("How Many Milk Do you want : ");
int milk = int.Parse(Console.ReadLine());
Console.Write("How Many Water Do you want : ");
int wa = int.Parse(Console.ReadLine());
Console.WriteLine("Coffee\t\t\t\t\t {0}\t\t\t\t\t\t{1:N}\t\t\t\tBath",coff,coff*30);
Console.WriteLine("Milk\t\t\t\t\t\t {0}\t\t\t\t\t\t{1:N}\t\t\t\tBath",milk,milk*15);
Console.WriteLine("Water\t\t\t\t\t\t{0}\t\t\t\t\t\t{1:N}\t\t\t\tBath",wa,wa*10);
int total1 = coff+milk+wa;
int total2 = coff*30+milk*15+wa*10;
Console.WriteLine("Total\t\t\t\t\t\t {0}\t\t\t\t\t\t{1:N}\t\t\t\tBaht",total1,total2);
Console.WriteLine("Total\t\t\t\t\t\t {0}\t\t\t\t\t\t{1:N}\t\t\t\tBaht",total1,total2+total2*0.07);
//vat หาได้จากการหาราคาทั้งหมดแล้ว+ราคาซ้ำแล้ว*0.07
int B1000,B500,B100,B50,B20,B10,B5,B1;
Console.WriteLine("Enter money you wish to convert :");
int money = int.Parse(Console.ReadLine());
B1000 = money / 1000;
B500 = money % 1000 / 500;
B100 = money % 1000 % 500 / 100;
B50 = money % 1000 % 500 % 100 / 50;
B20 = money % 1000 % 500 % 100 % 50 / 20;
B10 = money % 1000 % 500 % 100 % 50 % 20 / 10;
B5 = money % 1000 % 500 % 100 % 50 % 20 % 10 / 5;
B1 = money % 1000 % 500 % 100 % 50 % 20 % 10 % 5;
Console.WriteLine("Consvert to");
Console.WriteLine("One Thousand bath coins : {0}",B1000);
Console.WriteLine("Five hundred bath coins : {0}",B500);
Console.WriteLine("One hundred bath coins : {0}",B100);
Console.WriteLine("Fifty bath coins : {0}",B50);
Console.WriteLine("Twenty bath coins : {0}",B20);
Console.WriteLine("Ten bath coins : {0}",B10);
Console.WriteLine("Five bath coins : {0}",B5);
Console.WriteLine("One bath coins : {0}",B1);
int a ;
Console.WriteLine("Input you score");
a =int.Parse(Console.ReadLine());
if (a >= 80)
Console.WriteLine("You got Grade A");
}
else
Console.WriteLine("You got Grade B");
//จุดทศนิยมอย่าลืมใส่ f ไว้หลังเลข
int w;
float p;
Console.Write("โปรดใส่น้ำหนักของพัสดุ : ");
w = int.Parse(Console.ReadLine());
if(w >= 1 && w <= 300)
p = w * 0.1f;
else if(w >= 301 && w <= 500)
p = w * 0.15f;
else if(w >= 501 && w <= 1000)
p = w * 0.20f;
}else if(w >= 1001 && w <= 2000)
p = w * 0.25f;
}else
p = w * 0.3f;
Console.WriteLine("รวมราคาส่งพัสดุน้ำหนัก {0} กรับเท่ากับ {1} บาท",w,p);
Console.Write("Input your birth year : ");
int y = int.Parse(Console.ReadLine());
int year = 2563 - y;
if(year >= 20)
Console.WriteLine("Your age = {0}",year);
Console.WriteLine("Can Vote");
Console.WriteLine("Can't Vote");
//แบบที่ใช้เดดไทม์
int year = DateTime.Now.Year;
int yTH = year + 543;
int y1 = yTH - y;
if(y1 >= 20)
Console.WriteLine("Your age = {0}",y1);
}*/
Console.Write("Input number 1 : ");
int num1 = int.Parse(Console.ReadLine());
Console.Write("Input number 2 : ");
int num2 = int.Parse(Console.ReadLine());
Console.WriteLine("===========================================");
if(num1 > num2)
Console.WriteLine("Number 1 <{0}> is more than Number 2 <{1}>",num1,num2);
else if(num1 < num2)
Console.WriteLine("Number 1 <{0}> is less than Number 2 <{1}>",num1,num2);
else if(num1 == num2)
Console.WriteLine("Number 1 <{0}> is equals Number 2 <{1}>",num1,num2);
char sc;
int w,l;
float r;
Console.WriteLine("\t\t\t\t\tMENU"); //หรือเว้นแบบนี้ก็ได้ ("{0,20}","MENU")
Console.WriteLine("Press c or C : Find Area of Circle");
Console.WriteLine("Press s or S : Find Area of Square");
Console.WriteLine("Input your choice : ");
sc = char.Parse(Console.ReadLine());
if (sc == 's' || sc == 'S')
Console.WriteLine("You choose \"Square\"");
Console.Write("Input width : ");
Console.Write("Input length : ");
l = int.Parse(Console.ReadLine());
Console.WriteLine("Area = {0:F}",w*l);
else if (sc == 'c' || sc == 'C')
Console.WriteLine("You choose \"Circle\"");
Console.Write("Input radius : ");
r = float.Parse(Console.ReadLine());
Console.WriteLine("Area = {0:F}",Math.PI*(r*r));
//หรือจะหาแบบเลขเลยก็ได้ คือ 3.141f * r * r
Console.WriteLine("----------------------------------------------");
Console.Write("Input your number : ");
int num = int.Parse(Console.ReadLine());
if (num > 0 && num % 2 == 1)
Console.WriteLine("{0} is Positive Number, and Odd Number ",num);
else if (num > 0 && num % 2 == 0)
Console.WriteLine("{0} is Positive Number, and Even Number ",num);
else if (num < 0 && num % 2 == -1)
Console.WriteLine("{0} is Negative Number, and Odd Number ",num);
else if (num < 0 && num % 2 == 0)
Console.WriteLine("{0} is Negative Number, and Even Number ",num);
Console.WriteLine("{0} is Zero Number, and Even Number ",num);
//เลข + % 2 = 1 เลขคี่
//เลข + % 2 = 0 เลขคู่
//เลข - % 2 = -1 เลขคี่
//เลข - % 2 = 0 เลขคู่
Console.Write("Input total price : ");
int price = int.Parse(Console.ReadLine());
Console.Write("Do you have coupon [y/Y]or[n/N] :");
char coupon = char.Parse(Console.ReadLine());
if(coupon == 'y' && coupon == 'Y' )
Console.Write("How many discount <%> : ");
int dis_cou = int.Parse(Console.ReadLine());
Console.WriteLine("Discount from coupon {0}% = {1:N} Baht",dis_cou,price * dis_cou / 100);
Console.Write("Net Price = {0:N}",price-(price * dis_cou / 100));
else if (coupon == 'n' && coupon == 'N' && price >= 2000)
Console.WriteLine("Discount from promotion = {0:N} Baht",price*10/100);
Console.Write("Net Price = {0:N}",price-(price*10/100));
Console.WriteLine("Net Price = {0:N} Bath",price);
int num = 0;
int x = 0;
int min = 999999;
while (x < 8)
x++;
num = int.Parse(Console.ReadLine());
if(num < min)
min = num;
Console.Write("Min Number : {0}",min);
char sex;
do
Console.WriteLine("Enter Sex [M/F]");
sex = char.Parse(Console.ReadLine());
if(sex == 'M')
Console.Write("You are Male");
else if(sex == 'F')
Console.Write("You are Female");
}while(sex != 'M' && sex != 'F');
//1 Lap4
int input = 1;
do{
Console.Write("Input the Number ");
//ที่ทำอีฟนี้ขึ้นเพราะเราต้องการเพิ่มคำว่าอีกครั้งเข้าไปแต่จะเพิ่มในครั้งที่2 ฉะนั้นเงื่อไขของอันนี้คือไม่เอารอบแรกรอบอื่นเอาหมด
//เป็นเหมือนการหลอกโปรแกรมไม่ให้เขาตัวอีกครั้ง ในครั้งแรก หลักการคือมันจะข้ามขั้น ifไปเลย พอขั้นแรกผ่านไปตัวเลขใน input ล้างทำให้ตัวifกลับมาใช้งานได้
if(input <= 0){
Console.Write(", again");
Console.Write(": ");
input = int.Parse(Console.ReadLine());
}while (input <= 0);
int x = 1;
while(x <= 12 )
Console.WriteLine("{0} x {1} = {2}",input,x,input*x);
int num;
if( num % 2 == 1)
Console.WriteLine("I love C#");
}while(x <= num );
else if (num % 2 == 0)
while( x <= 12)
Console.WriteLine("{0} x {1} = {2}",num,x,num*x);
int temper;
char system,sky;
Console.Write("Enter temperature : ");
temper = int.Parse(Console.ReadLine());
Console.Write("All system go <Y/N> : All system go <Y/N> : ");
system = char.Parse(Console.ReadLine());
}while(system != 'Y' && system != 'N');
Console.Write("Sky confirm <P,C,R,T> : Sky confirm <P,C,R,T> : ");
sky = char.Parse(Console.ReadLine());
}while(sky != 'P' && sky !='C' && sky !='R' && sky !='T');
if(system == 'Y' && (sky == 'C' || sky == 'P') && temper >= 60 && temper <= 90)
Console.Write("Lanch Now!!");
Console.Write("Do Not Lanch !!");
int stu;
float score,min = 99999f,max = 0f,sum = 0 ;//sum เอาไว้เก็บค่าเอาไว้คิดค่าเฉลี่ยก็ได้
Console.WriteLine("=======================================");
Console.Write("How many student in class : ");
stu = int.Parse(Console.ReadLine());
}while(stu <= 0);
//loopเพื่อใส่คะแนนของนักเรียน
while( x <= stu )
Console.Write("Input score of student {0} : ",x);
score = float.Parse(Console.ReadLine());
if(score >= 0 && score <= 100)
x++; //เป็นตัวสำคัญที่ทำให้เลขเปลี่ยนจึงเอามาซ่อนโดย if ทีมีเงื่อนไขของคะแนนที่อยากให้มีคะแนนเท่าไร พอไม่ถูตามเงื่อมันก็จะไม่เพิ่มค่าให้
sum += score; //เป็นการเอาคำตอบที่กรอกทุกอันเอามาบวกรวมกันให้หมด
//ที่ใช้ if ซ้อนกันก็เพื่อนมันจะได้คิดทุกอัน แล้วเอาออกมาใช้ได้
if (score > max)
max = score;
if (score < min)
min = score;
//else if
Console.WriteLine("Average score = {0:F}",sum/(x-1));
Console.WriteLine("Maximum score = {0:F}",max);
Console.WriteLine("Minimun score = {0:F}",min);
//Lab5
/*//1
int num = 2;
Console.Write("Enter Number :");
}while(num > 12 || num < 2);
for(int x = 1; x <= 12; x++)
Console.WriteLine("{0} * {1} = {2}",num,x,num*x);
//2
int x , number, num = 0,even = 0,odd=0,sumodd = 0,sumeven = 0;
Console.Write("How many number you want to input : ");
number = int.Parse(Console.ReadLine());
Console.WriteLine("*****************************************");
for(x = 1 ; x <= number; x++){ //อย่าลืมดู {} ให้ถูกที่ไม่งั้นมันจะทำแค่ 1 ครั้ง
Console.Write("Input Number {0} : ",x);
if(num % 2 == 0)
sumeven += num;
even++; //สามารถใช้แบบนี้ได้เพราะจะนับเพิ่มเมื่อเข้าเงื่อนไขนี้
sumodd += num;
odd++;
Console.WriteLine("Have EVEN number {0} of {1}",even,number);
Console.WriteLine("SUM OF EVEN NUMBER = {0}",sumeven);
Console.WriteLine("");
Console.WriteLine("Have ODD number {0} of {1}",odd,number);
Console.WriteLine("SUM OF ODD NUMBER = {0}",sumodd);
Console.Write("*****************************************");
//3
//เอาราคาคูณเปอร์หาร100เพื่อหาราคาลด price*deper/100
Console.Write("Input Price of car :");
Console.Write("Input Depreciation per year <%> :");
int deper = int.Parse(Console.ReadLine());
Console.Write("Input How many year you want to see :");
Console.WriteLine("-----------------------------------------------------------------");
Console.WriteLine("Price of Car = {0:N} Baht",price);
for (int x = 1; x<=y; x++){
Console.WriteLine("AFTER USE {0} YEAR : REDUCE = {1:N} BAHT PRICE = {2:N} BAHT",x,price*deper/100, price -= price*deper/100);
//4
//for สามารถสร้างได้ 2 ทางหรือมากกว่านั้น โดยใช้ตัวแปรแทนด้านนั้นๆ
for(int x = 1;x <= 9;++x){//แนวตั้ง
Console.Write(x);
for(int y = x + 1; y <= 9;++y){//แนวนอน & ที่เอา x+1 เพราะเราจะทำให้จำนวนมันเรียงกันโดยจะ + จากจำนวน x ในชั้นนั้นไปในแนวนอนหากไม่ทำจำนวน y จะเริ่มนับที่ 1
Console.Write(y);
Console.WriteLine();
int x = 12;
int y = 1;
while(y<=12){
Console.WriteLine("{0} x {1} = {2}",x,y,x*y);
y++;