280 likes | 451 Views
C# Code Exercise. Suphot Sawattiwong tohpus@gmail.com. Ex1. int a = 9, b = 4; int t = 0; Console .WriteLine(a + "," + b); t = a; a = b; b = t; Console .WriteLine(a + "," +b); Console .ReadLine ();. Ex1_1. int a = 9, b = 4; Console .WriteLine(a + "," + b); a = a^b;
E N D
C# Code Exercise SuphotSawattiwong tohpus@gmail.com
Ex1 int a = 9, b = 4; int t = 0; Console.WriteLine(a +"," + b); t = a; a = b; b = t; Console.WriteLine(a+","+b); Console.ReadLine();
Ex1_1 int a = 9, b = 4; Console.WriteLine(a +"," + b); a = a^b; b= a^b; a= a^b; Console.WriteLine(a+","+b); Console.ReadLine();
Ex1_2 int a = 9, b = 4; Console.WriteLine(a +"," + b); a=a*b; b=a/b; a=a/b; Console.WriteLine(a+","+b); Console.ReadLine();
Ex1_3 int a = 9, b = 4; Console.WriteLine(a +"," + b); a = a - b; b = a + b; a = b - a; Console.WriteLine(a+","+b); Console.ReadLine();
Ex2 int a = 4, b=8; int max=0, min= 0; if(a<b){ max=b; min = a; }else{ max = a; min = b; } Console.WriteLine(max+">"+ min); Console.ReadLine();
Ex3 int c = 0; while (c < 10) { Console.WriteLine(c); c++; } Console.ReadLine();
Ex4 int c = 0; while (c < 10) { Console.WriteLine(c++); } Console.ReadLine();
Ex5 int c = 0; while (c < 10) { Console.WriteLine(++c); } Console.ReadLine();
Ex6 int sum = 0; int i = 0; while (i < 10) { sum += i; if (i != 0) { Console.Write("+"); } Console.Write(i); i++; } Console.WriteLine(); Console.WriteLine("Sum ={0}", sum); Console.ReadLine();
Ex7 int s = 0; int i = 0; while (i < 10) { s += i; if (i != 0) { Console.Write("+"); } Console.Write(i); i=i+2; } Console.WriteLine(); Console.WriteLine("s ={0}", s); Console.ReadLine();
Ex8 for(int i=0; i<5; i++){ if(i%2==0) Console.Write(i); else Console.Write(" "); } Console.ReadLine();
Ex9 int a = 5,f = 1; Console.Write("{0}!=", a); for (; a > 0; a--) { f = f * a; Console.Write(a); if (a != 1) Console.Write("*"); } Console.WriteLine("={0}",f); Console.ReadLine();
Ex10 int i = 1234567; while (i > 0) { Console.WriteLine(i % 10); i = i / 10; } Console.ReadLine();
Ex11 int i=1234567; string s=""; while(i>0){ s= ""+(i%10)+" "+ s; i=i/10; } Console.WriteLine(s); Console.ReadLine();
Ex12 int a = 4, b = 5, c = 7; int min = 0; if (a < b) min = a; if (min > c) min = c; Console.WriteLine(min); Console.ReadLine();
Ex13 int a = 2,b = 5,c=1; while (a <= b){ Console.Write(" "+a+" "); a++; } Console.WriteLine();
Ex13(ต่อ) do { a = 2; do { Console.Write("{0,2}*{1,2}={2,2} ", a, c, (a * c)); a++; }while (a <= b); Console.WriteLine(); c++; } while (c <= 12); Console.ReadLine();
Ex14 int i = 1024; int c = 0; while (i>0) { Console.WriteLine("i=" + i); i = i >> 1; c++; } Console.WriteLine("c=" + c); Console.ReadLine();
Ex15 char a ='A'; Console.WriteLine((char)(a + 32)); Console.ReadLine();
Ex16 char a ='A'; for(int i=0;i<26;i++){Console.WriteLine(a++); } Console.ReadLine();
Ex17 char a ='0'; int b = a; Console.WriteLine(b-48);Console.ReadLine();
Ex18 int a = 3; switch (a) { case 1: Console.Write("Hello"); break; case 2: Console.Write(" ^_^ \n"); break; case 3: case 4: case 5: Console.Write(" (\",) \n\n"); Console.Write(" (@ _ @) "); break; default: Console.Write(" Default "); break; } Console.ReadLine();
Ex19 string s ="Hello"; int offset = (80 - s.Length)>>1; for (int i = 0; i < offset; i++) Console.Write(" "); Console.WriteLine(s); Console.ReadLine();
Ex19_1 string s ="Hello"; int offset = (80 - s.Length)/2; for (int i = 0; i < offset; i++) Console.Write(" "); Console.WriteLine(s); Console.ReadLine();
Ex19_2 string s ="Hello"; int offset = (80 - s.Length); for (int i = 0; i < offset; i++) Console.Write(" "); Console.WriteLine(s); Console.ReadLine();
Ex20 int a = 1; int b = 1; for (int i = 0; i < 5; i++) { if (i != 0) { b = b * 10; a = a + b; } strings ="" + (a * a); int c=(80-s.Length)/2; for (int j = 0; j < c; j++) Console.Write(" "); Console.WriteLine(s); } Console.ReadLine();