C căn bản và xuất nhậpViết chương trình để so sánh 2 số thực chính xác đến 0.000001. Examples: (5.3 ; 6.01) false; (5.00000001 ; 5.00000003) trueKhai báo hai biến số nguyên và gán chúng với 5 và 10 và sau khi trao đổi có giá trị của chúng.
Trang 1Bài tập
1 Viết chương trình để so sánh 2 số thực chính xác đến 0.000001 Examples: (5.3 ; 6.01) false; (5.00000001 ; 5.00000003) true
Console.WriteLine(" nhập 2 số thực");
double a= double.Parse(Console.ReadLine()); double b= double.Parse(Console.ReadLine());
if (a < b) { Console.Write(" " +a);
Console.Write(" < ");
Console.Write( " " + b);}
else {Console.Write(" "+a);
Console.Write(" > ");
Console.Write( " " + b);}
Console.ReadLine();
Trang 2}
2 Khai báo hai biến số nguyên và gán chúng với 5 và 10
và sau khi trao đổi có giá trị của chúng.
int a,b, tam;
a = 5 ;
b = 10;
tam = a;
a = b ; b= tam;
tam = b;
b= a;
a = tam;
Console.WriteLine(" a ="+ a);
Console.WriteLine("b = "+ b);
Console.ReadLine();
3.Viết chương trình in ra một tam giác cân của 9 ký
hiệu bản quyền © Sử dụng Windows Character Map để
Trang 3static void Main(string[] args)
{
Console.WriteLine("nhap chieu cao tam giac:"); int h = int.Parse(Console.ReadLine());
int a = 1 + (h - 1) * 2;
int i = 1;
int j = 1;
for(i=1;i<=h;i++)
if(i<h)
{
for(j=1;j<=a;j++)
if((j==(((a+1)/2)+(i-1))||j==(((a+1)/2)-(i-1))))
Console.Write("*");
else Console.Write(" ");
Console.WriteLine();
Trang 4}
else
for(j=1;j<=a;j++)
Console.Write("*");
Console.ReadLine();
}
4 Viết chương trình nhập vào tên,năm sinh 1 người bất
kỳ Sau đó in lên màn hình các kết quả sau:tên,năm
sinh,tuổi hiện tại và tuổi năm 2000 của họ.
Console.WriteLine(" nhập tên");
string ten = Console.ReadLine();
Console.WriteLine(" nhập năm sinh");
int n = int.Parse(Console.ReadLine());
int a = DateTime.Now.Year;
if ( a<=n)
{Console.WriteLine(" bạn nhập sai năm sinh");}
Trang 5c =(2000 - n) ;
int Tuoi = a-n;
Console.WriteLine(" tên :"+ten);
Console.WriteLine(" năm sinh :"+n);
Console.WriteLine(" tuổi hiện tại: "+Tuoi);
Console.WriteLine(" tuổi năm 2000: "+c);}
Console.ReadLine();
5 Viết chương trình kiểm tra 1 có thể chia hết 5 và 7 không.
Console.WriteLine(" nhập số a");
int a = int.Parse( Console.ReadLine());
int b,c;
b = a % 5;
c = a % 7;
Trang 6if (( b==0) && (c == 0))
{ Console.WriteLine(" a chia hết cho 5 và 7");}
else Console.WriteLine(" a không chia hết cho 5 và 7");
Console.ReadLine();
7.Viết chương trình cho phát ra một bản nhạc mà bạn thích
using System;
class Organ
static readonly String[] note= {"Do","Do #,"Re","Re#",
"Mi","Fa","Fa#","Sol","Sol#","La","La#","Si"};
static readonly string[] key= new string[]
{"A","B","C","D","E","F","G"," H","I" ,"K","L","M"};
const int width=12, height=15, col=15, row=5;
static void DrawAKey(int x, int y, int w, int h,
ConsoleColor color, string s, string t)
{
for(int i=x;i<=x+w;i++)
Trang 7{
Console.SetCursorPosition(i,j);
if (i!=x && i!=x+w && j!=y && j!= y+h)
Console.BackgroundColor=color;
else
Console.BackgroundColor=ConsoleColor.Green; Console.Write(Nhập""Do","Do #,"Re","Re#",
"Mi","Fa","Fa#","Sol","Sol#","La","La#","Si"");
}
Console.BackgroundColor=color;
Console.ForegroundColor=ConsoleColor.Blue;
Console.SetCursorPosition(x+w/2,y+h-3);
Console.Write(s);
Console.SetCursorPosition(x+w/2,y+h-2);
Console.Write(t);
}
Trang 8static void DrawKeyBoard ()
{
Console.BackgroundColor=ConsoleColor.Black; Console.Clear();
for(int i=1;i<=7;i++) DrawAKey(col+(i-1)*width, row, width, height, ConsoleColor.Red,note[i-1], key[i-1]); Console.BackgroundColor=ConsoleColor.Black;
}
static void Sound(int fre)
{
int delay=300;
Console.Beep(fre,delay);
}
public static void Main()
{
DrawKeyBoard();
ConsoleKeyInfo k;
Trang 9{
k = Console.ReadKey(true);
switch (k.Key)
{
case ConsoleKey.A:Sound(523); break; case ConsoleKey.B:Sound(554); break; case ConsoleKey.C:Sound(587); break; case ConsoleKey.D:Sound(622); break; case ConsoleKey.E:Sound(659); break; case ConsoleKey.F:Sound(699); break; case ConsoleKey.G:Sound(740); break; case ConsoleKey.H:Sound(784); break; case ConsoleKey.I:Sound(831); break; case ConsoleKey.K:Sound(880); break; case ConsoleKey.L:Sound(932); break; case ConsoleKey.M:Sound(988); break;
Trang 10}
}
Console.ReadLine(); }
}