Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# ทำการคำนวณเลขคณิตพื้นฐาน

ตัวดำเนินการเลขคณิตพื้นฐานใน C#:

ตัวดำเนินการคำอธิบาย
+บวกสองอпеรานด์
-ลบอเปรานด์ที่สองจากอเปรานด์แรก
*คูณสองอเปรานด์
/หารตัวเศษด้วยตัวส่วน
%Modulo - หาเศษจากการหารจำนวนเต็ม
++Increment - เพิ่มค่าจำนวนเต็มขึ้น 1
--Decrement - ลดค่าจำนวนเต็มลง 1

ตัวอย่างโค้ดเต็ม

using System;
namespace Sample {
    class Demo {
        static void Main(string[] args) {
            int num1 = 50;
            int num2 = 25;
            int result;
            result = num1 + num2;
            Console.WriteLine("Value is {0}", result);  // 75
            result = num1 - num2;
            Console.WriteLine("Value is {0}", result);  // 25
            result = num1 * num2;
            Console.WriteLine("Value is {0}", result);  // 1250
            result = num1 / num2;
            Console.WriteLine("Value is {0}", result);  // 2
            result = num1 % num2;
            Console.WriteLine("Value is {0}", result);  // 0
            result = num1++;
            Console.WriteLine("Value is {0}", result);  // 50 (post-increment)
            result = num1--;
            Console.WriteLine("Value is {0}", result);  // 51 (post-decrement)
            Console.ReadLine();
        }
    }
}

ผลลัพธ์

Value is 75
Value is 25
Value is 1250
Value is 2
Value is 0
Value is 50
Value is 51

หมายเหตุ: num1++ และ num1-- เป็น post-increment/decrement คืนค่าก่อนเพิ่ม/ลด