C-Program การคำนวณ

ตัวดำเนินการ

+ บวก

 –  ลบ

 * คูณ

 /  หาร

 % หารเอาเศษ (modulus)

ลำดับความสำคัญ

1.  – เอกภาพลบ (เช่น -1)

2.  * , / คูณและหาร

3.  +, – บวกและลบ

ตัวอย่างโปรแกรม

 
 #include <stdio.h>
 int main(){
       int x = 5 , y=10 , z;
       z = x+y;
       printf(" x + y = %d\n", z);
       printf(" x + y = %d\n", x+y);
       printf(" x - y = %d\n", x-y);
       printf(" x * y = %d\n", x+y);
       z = x-2*y/2-5+3-x;
       printf(" x-2*y/2-5+3-x = %d\n", z);
  
 return 0;
 } 

ตัวอย่างโปรแกรมคำนวณ คะแนนรวม

 #include <stdio.h> 
 int main(){
        char name[20];
        int score,s1,s2,s3,sfinal;
        printf("Enter your name :");
        scanf("%s",name);
        printf("Enter your score1(30):");
        scanf("%d",&s1);
        printf("Enter your score2(30):");
        scanf("%d",&s2);
        printf("Enter your score3(30):");
        scanf("%d",&s3);
        printf("Enter your final(20):");
        scanf("%d",&sfinal);
        printf("---------------------------------------\n");
        score = s1+s2+s3+sfinal;
        printf("Your name is %s\n",name);
        printf("Your Score is %d + %d + %d + %d = %d\n",s1,s2,s3,sfinal,score);
        printf("---------------------------------------\n");
 return 0;
  }

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *