Computer >> Máy Tính >  >> Lập trình >> C#

Chương trình C # để tìm tối đa ba số


Trước tiên, hãy đặt ba số -

int num1, num2, num3;
// set the value of the three numbers
num1 = 10;
num2 = 20;
num3 = 50;

Bây giờ hãy kiểm tra số đầu tiên với số thứ hai. Nếu num1> num2, thì kiểm tra num1 với num3. Nếu num1 lớn hơn num3, điều đó có nghĩa là số lớn nhất là num1.

Ví dụ

Bạn có thể thử chạy mã sau để tìm tối đa ba số.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo {
   class MyApplication {
      static void Main(string[] args) {
         int num1, num2, num3;
         // set the value of the three numbers
         num1 = 10;
         num2 = 20;
         num3 = 50;
         if (num1 > num2) {
            if (num1 > num3) {
               Console.Write("Number one is the largest!\n");
            } else {
               Console.Write("Number three is the largest!\n");
         }
      }
      else if (num2 > num3)
         Console.Write("Number two is the largest!\n");
      else
         Console.Write("Number three is the largest!\n");
      }
   }
}

Đầu ra

Number three is the largest!