Mô tả bài toán
Tạo ứng dụng Console, cho phép người dùng nhập vào 1 con số. Kiểm tra và in ra màn hình con số vừa nhập là chẵn hay lẻ
Cách giải quyết
Lấy con số người dùng nhập lưu vào biến
i, kiểm tra giá trị của biểu thức chia lấy dư cho 2
i % 2, nếu:
i % 2 == 0: là số chẵn.
i % 2 != 0: là số lẻ.
Source code
[su_spoiler title="Bài giải (Nên nhớ tự làm trước khi click vào đây)"]
1/*
2C# Program to Check whether the Entered Number is Even or Odd
3This is a C# Program to check whether the entered number is even or odd.
4
5Problem Description
6This C# Program checks if a given integer is Odd or Even.
7
8Problem Solution
9Here if a given number is divisible by 2 with the remainder 0 then the number is an Even number. If the number is not divisible by 2 then that number will be an Odd number.
10*/
11using System;
12using System.Collections.Generic;
13using System.Linq;
14using System.Text;
15
16namespace Lession1
17{
18 class Program
19 {
20 static void Main(string[] args)
21 {
22 int i;
23 Console.Write("Enter a Number : ");
24 i = int.Parse(Console.ReadLine());
25 if (i % 2 == 0)
26 {
27 Console.Write("Entered Number is an Even Number");
28 Console.Read();
29 }
30 else
31 {
32 Console.Write("Entered Number is an Odd Number");
33 Console.Read();
34 }
35 }
36 }
37}
[/su_spoiler]
Github
https://github.com/kellyfire611/learning.nentang.vn-csharp/tree/master/src/Lession1