วันอาทิตย์ที่ 5 ธันวาคม พ.ศ. 2553

โค๊ดภาษา c ตอน switch-case

ตัวอย่าง switch-case 


#include <stdio.h>

               void main(){

                      int x,y;
                      char sym;

                      printf("\t\tProgram calculator 2 digit.\n");
                      printf("choice(+, -, *, /, %) : ");
                      scanf("%c",&sym);
                      printf("Input number1 : ");
                      scanf("%d",&x);
                      printf("Input number1 : ");
                      scanf("%d",&y);


                      switch(sym){
                              case '*' :
                                 printf("Result = %d\n",x*y);
                                 break;
                             case '-' :
                                 printf("Result = %d\n",x-y);
                                 break;
                             case '+' :
                                 printf("Result = %d\n",x+y);
                                 break;
                             case '%' :
                                 printf("Result = %d\n",x%y);
                                 break;
                             case '/' :
                                 printf("Result = %d\n",x/y);
                                 break;

                         default : printf("Error!\n");

          }
}






หมายเหตุ  *-* แลกเปลี่ยนกันได้ครับ หรือถามเกี่ยวกันการเขียนโปรแกรมได้ครับ

1 ความคิดเห็น:

แสดงความคิดเห็นด้วยถ้อยคำสุภาพครับ เพื่อสังคมไทยของเรา แบ่งปันสิ่งดีดี