Đây là chương trình C# được dùng để kiểm tra một năm là năm nhuận hay không, bài tập này cũng khá thông dụng khi mới bắt đầu học C-Sharp. Công thức tính năm nhuận là lấy số năm chia cho 4, nếu nó chia hết cho 4 thì năm đó gọi là năm nhuận.
Mã Nguồn C# - C# Source Code:
Mã Nguồn C# - C# Source Code:
----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpExample
{
class leapyear
{
static void Main(string[] args)
{
leapyear obj = new leapyear();
obj.readdata();
obj.leap();
}
int y;
public void readdata()
{
Console.WriteLine("Enter the Year in Four Digits : ");
y = Convert.ToInt32(Console.ReadLine());
}
public void leap()
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
Console.WriteLine("{0} is a Leap Year", y);
}
else
{
Console.WriteLine("{0} is not a Leap Year", y);
}
Console.ReadLine();
}
}
}
----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpExample
{
class leapyear
{
static void Main(string[] args)
{
leapyear obj = new leapyear();
obj.readdata();
obj.leap();
}
int y;
public void readdata()
{
Console.WriteLine("Enter the Year in Four Digits : ");
y = Convert.ToInt32(Console.ReadLine());
}
public void leap()
{
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
{
Console.WriteLine("{0} is a Leap Year", y);
}
else
{
Console.WriteLine("{0} is not a Leap Year", y);
}
Console.ReadLine();
}
}
}
----------------------
No comments:
Post a Comment