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

Arithmetic Operators ในภาษา C: + - * / %

Arithmetic operators ใช้คำนวณคณิตศาสตร์พื้นฐาน

ตาราง Operators

OperatorDescriptionExamplea=20,b=10Output
+Additiona+b20+1030
-Subtractiona-b20-1010
*Multiplicationa*b20*10200
/Divisiona/b20/102 (quotient)
%Moduloa%b20%100 (remainder)

อัลกอริทึม

START
Step 1: ประกาศตัวแปร int
Step 2: รับค่าทุกตัวแปร
Step 3: คำนวณ a+b, a-b, a*b, b/a, a%b
Step 4: พิมพ์ผลลัพธ์

โปรแกรม

#include <stdio.h>
main() {
    int a, b;
    printf("Enter a, b: ");
    scanf("%d %d", &a, &b);
    printf("a+b = %d\n", a+b);
    printf("a-b = %d\n", a-b);
    printf("a*b = %d\n", a*b);
    printf("b/a = %d\n", b/a);
    printf("a%%b = %d\n", a%b);
}

ผลลัพธ์

Enter a, b: 40 60
a+b = 100
a-b = -20
a*b = 2400
b/a = 1
a%b = 40