You are on page 1of 33

เฉลยแบบฝึกหัด : การรับค่าและแสดงผล

1. หาพื้นที่วงกลมและเส้นรอบวงของวงกลมโดยรับค่ารัศมีทางแป้นพิม์และแสดงผลลัพธ์
#include<stdio.h>
void main()
{
float radius, area, circum;
printf("Please enter radius of cycle : ");
scanf("%f", &radius);
area = 3.14 * radius * radius;
circum = 2 * 3.14 * radius;
printf("Area of cycle : %.2f\n", area);
printf("Circumference of cycle : %.2f", circum);
}

2. เปลี่ยนอุณหภูมิจากองศาเซลเซียสเป็นฟาเรนต์ไฮท์ โดยป้อนอุณหภูมิเป็นองศาเซลเซียส ทางแป้นพิมพ์


แล้วแสดงผลลัพธ์ทางจอภาพ
#include<stdio.h>
main() {
float celsius, fahrenheit;
printf("Please enter Celsius degree : ");
scanf("%f", &celsius);
fahrenheit = ((9/5) * celsius) + 32;
printf("%.2f C degree is equal %.2f F Degree\n", celsius, fahrenheit);
}
3. รับข้อมูลพื้นที่นา ของชาวนา 1 คน โดยรับจานวนไร่, จานวนงาน, จานวนตารางวา แล้วเปลี่ยนหน่วยให้
เป็น เอเคอร์ แล้วแสดงผลลัพธ์ทางจอภาพ
#include<stdio.h>
void main() {
float A, B, C, Acre;
printf("Enter Rai : ");
scanf("%f", &A);
printf("Enter Gharn : ");
scanf("%f", &B);
printf("Enter TarangWah : ");
scanf("%f", &C);
Acre = ((A * 1600) + (B * 400) + (C * 4))/4046.874;
printf("This Area is equal %.2f Acre\n", Acre);
}

4. คานวณหาพื้นที่ของสี่เหลี่ยมรูปว่าว แล้วแสดงผลลัพธ์
#include<stdio.h>
main(void) {
float diagonal1, diagonal2, Area;
printf("Enter Diagonal 1 : ");
scanf("%f", &diagonal1);
printf("Enter Diagonal 2 : ");
scanf("%f", &diagonal2);
Area = 0.5 * (diagonal1 + diagonal2);
printf("Area of triangle-kite : %.2f\n", Area);
}
5. คานวณหาค่าไฟฟ้า จากสูตร ค่าไฟฟ้า = จานวนหน่วยที่ใช้ * ราคาต่อหน่วย โดยค่าไฟในปัจจุบันหน่วยละ
2.50 บาท แล้วแสดงผลลัพธ์
#include<stdio.h>
main() {
float unit_price = 2.50, AC_units, electricity_charge;
printf("Enter AC Units : ");
scanf("%f", &AC_units);
electricity_charge = 2.50 * AC_units;
printf("Electricity Charge : %.2f\n", electricity_charge);
}

6. หาพื้นที่สี่เหลี่ยมใดๆ แล้วแสดงผลลัพธ์
#include<stdio.h>
void main() {
float diagonal, base1, base2, area;
printf("Enter Diagonal : ");
scanf("%f", &diagonal);
printf("Enter Base1 : ");
scanf("%f", &base1);
printf("Enter Base2 : ");
scanf("%f", &base2);
area = 0.5 * diagonal * (base1 + base2);
printf("Area of Square : %.2f\n", area);
}
7. หาพื้นที่ผิวทรงกระบอก แล้วแสดงผลลัพธ์
#include<stdio.h>
main(void) {
float radius, height, surface_area;
printf("Enter Radius of Cylinder : ");
scanf("%f", &radius);
printf("Enter Height of Cylinder : ");
scanf("%f", &height);
surface_area = ((2 * 3.14 * radius) * height) + (2 * (3.14 * radius * radius));
printf("Surface area of Cylinder : %.2f\n", surface_area);
}

8. เปลี่ยน พ.ศ ให้เป็น ค.ศ และ จ.ส โดยให้รับ พ.ศ. ทางแป้นพิมพ์ แล้วแสดงผลลัพธ์
#include<stdio.h>
void main() {
int BE, CE, TE;
printf("Enter BE year : ");
scanf("%d", &BE);
CE = BE - 543;
TE = BE -1181;
printf("Anno Domini = %d\n", CE);
printf("Buddhist Era = %d", TE);
}
เฉลยแบบฝึกหัด : เครื่องหมายและการดาเนินการในภาษา C

1. จงหาผลลัพธ์ โดยกาหนดให้ a , b, c, d แทนด้วยรหัสนิสิต 4 ตัวท้าย


1. -5 + 4 * (20 % 6) = 3
วิธีทา
1) (20 % 6)
2) 4 * 1)
3) -5 + 2)

2. (-5 * (192 / 3) + 7) – 4 % 10 * 2 = -321


วิธืทา
1) 192 / 3
2) -5 * 1)
3) 2) + 7
4) 4 %10
5) 4) * 2
6) 3) – 5)

3. -10 + (++a) – 7 + 3 * 2 % 7 * 2 = 1 (สมมุติให้ a =5)


วิธีทา
1) (++a)
2) 3 * 2
3) 2) % 7
4) 3) * 2
5) -10 + 1)
6) 5) – 7
7) 6) + 4)
4. ++b + d-- % 2 * (++c * 4 – 1) / 2 = 22 (สมมุติให้ b =6, c = 7, d=8)
วิธีทา
1) ++c
2) 1) * 4
3) 2) – 1
4) ++b
5) d--
6) 5) % 2
7) 6) * 3)
8) 7) /2
9) 4) + 8)

5. 2 * 5 % 3 / 2 * (++c * 4 – 1) /2 = 0.00 (สมมุติให้ c = 7)


วิธีทา
1) ++c
2) 1) * 4
3) 2) – 1
4) 2 * 5
5) 4) % 3
6) 5) / 2
7) 6) * 3)
8) 7) / 2
2. จงหาค่าของนิพจน์ต่อไปนี้
1. 4 | 2 & 5
= 00000100 | 00000010 & 00000101
= 00000100 | 00000000
= 00000100
2. 3 | 5 | 4 & 255
= 00000011 | 00000101 | 00000100 & 11111111
= 00000011 | 00000101 | 00000100
= 00000111 | 00000100
= 00000111
3. 4 ^ 2 & 5
= 00000100 ^ 00000010 & 00000101.
= 00000100 ^ 00000000
= 00000100
4. 13 << 2
= 00001101 << 2
= 00110100
= 52
5. 22 << 4 >> 3
= 00010110 << 4
= 01100000 >> 3
= 00001100
= 12
3. จากการประกาศตัวแปรในภาษา C ดังนี้
int i = 8, j = 5;
float x = 0.005, y = -0.01;
char c = ‘c’, d = ‘d’;
จงหาผลลัพธ์ต่อไปนี้
1. c > d = False หรือ F หรือ 0
2. –(i + j) = -13
3. ++x = 1.005 (x++ = 0.005 ในกรณีเขียนโปรแกรม)
4. ++i = 9 (i++ = 8 ในกรณีเขียนโปรแกรม)
5. y-- = -0.010 (--y = -1.010 ในกรณีเขียนโปรแกรม)
6. --j = 4 (j-- = 5 ในกรณีเขียนโปรแกรม)
7. (3 * i – 2 * j) % (2 * d – c) = 14
8. 2 * ((i / 5) + (4 * (j – 3)) % (i + j – 2) = 18
9. x >= 0 = True หรือ T หรือ 1
10. c == 99 = True หรือ T หรือ 1
11. j != 6 = True หรือ T หรือ 1
12. !(i <= j) = True หรือ T หรือ 1
13. (i > 0) && (j < 5) = False หรือ F หรือ 0
14. (i > 0) || (j < 5) = True หรือ T หรือ 1
15. (x > y) && (i > 0) || (j < 5) = True หรือ T หรือ 1
4. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม

ผลจากโปรแกรม
อธิบายโปรแกรม
โปรแกรมทดสอบการเปลี่ยนชนิดของตัวแปรที่มีการดาเนินการของตัวแปรคนละชนิดกัน คือ
b = b + a = int + char = int
c = c + a = float + char = float
d = d + a = double + char = double
c มีคา่ ตามตารางรหัส ASCII คือ 99 จึงสามารถบวกค่ากับ ตัวแปรชนิด int, float, double ได้

5. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม

ผลจากโปรแกรม
อธิบายโปรแกรม
โปรแกรมทดสอบการเปลี่ยนชนิดของตัวแปรที่มีการดาเนินการของตัวแปรคนละชนิดกัน ซึ่งเป็นการเปลี่ยนโดยผู้ใช้เอง
โดยกาหนดชนิดของตัวแปรไว้หน้าตัวแปรที่ตอ้ งการเปลี่ยนชนิดของตัวแปร

6. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม
#include<stdio.h>
void main( ) {
int x, y, z;
printf(“Enter x : ”);
scanf(“%d”, &x);
printf(“Enter y : ”);
scanf(“%d”, &y);
printf(“x + y = %d\n”, x + y);
printf(“x - y = %d\n”, x - y);
printf(“x * y = %d\n”, x * y);
printf(“x / y = %d\n”, x / y);
printf(“x mod y = %d\n”, x % y);
}

ผลจากโปรแกรม

อธิบายโปรแกรม
โปรแกรมรับค่าจานวนเต็มสองจานวน เพื่อนาไปคานวณทางคณิตศาสตร์ คือ +, -, *, / และ %
7. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม
#include<stdio.h>
main( ) {
int a = 9, b = 6, c = 0;
c = (a++) + (++b);
printf(“%d %d %d\n”, a, b, c);
c = (--a) + (--b);
printf(“%d %d %d\n”, a, b, c);
c = ++a;
c = c – b--;
c = c + --a;
c = c - ++b;
printf(“%d %d %d\n”, a, b, c);
}

ผลจากโปรแกรม
10 7 16
9 6 15
9 6 7

อธิบายโปรแกรม
โปรแกรมทดสอบ Unary Operators คือ --, ++ ว่ามีการทางานอย่างไร ระหว่างมี ( ) และไม่มี ( )
8. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม

ผลจากโปรแกรม

อธิบายโปรแกรม
โปรแกรมแสดงผลการทางานของตัวดาเนินการประเภท Comparison คือ >, >=, ==, != และ Logical คือ &&, ||, !
9. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม
#include<stdio.h>
main( ) {
int p, q;
printf("***Results of logical operators***\n");
printf(" p | q | p && q | p || q | !p | !q \n");
printf("******************************\n");
p = 2; q = 3;
printf(" %d | %d | %d | %d | %d | %d \n", p, q, p&&q, p||q, !p, !q);
p = 1; q = 0;
printf(" %d | %d | %d | %d | %d | %d \n", p, q, p&&q, p||q, !p, !q);
p = 0; q = 5;
printf(" %d | %d | %d | %d | %d | %d \n", p, q, p&&q, p||q, !p, !q);
p = 0; q = 0;
printf(" %d | %d | %d | %d | %d | %d \n", p, q, p&&q, p||q, !p, !q);
}

ผลจากโปรแกรม

อธิบายโปรแกรม
โปรแกรมแสดงผลการทางานของตัวดาเนินการประเภท Logical คือ &&, ||, !
10. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม
#include<stdio.h>
main() {
printf(“Bitwise Operation”);
printf(“Result of expression 10 & 2 is %d\n\”, 10 & 2);
printf(“Result of expression 10 | 2 is %d\n”, 10 | 2);
printf(“Result of expression 10 ^ 2 is %d\n”, 10 ^ 2);
printf(“Result of expression ~10 is %d\n”, ~10);
printf(“Result of expression ~2 is %d\n”, ~2);
printf(“Result of expression 10 >> 2 is %d\n”, 10 >> 2);
printf(“Result of expression 10 << 2 is %d\n”, 10 << 2);
}

ผลจากโปรแกรม

อธิบายโปรแกรม
โปรแกรมแสดงผลการทางานของตัวดาเนินการประเภท Bitwise คือ &, |, ^, ~, <<, >> โดยการเลื่อนบิตไปทางซ้ายและ
ขวาตรงกับข้อสังเกตของ การเลื่อนบิตจริง คือ << ค่าที่ได้จะเหมือนกับ การคูณ 2 กับค่าที่ทาการเลื่อนบิตตามจานวน
บิตที่ตอ้ งการ ตามโปรแกรม คือ 10 << 2 คือ เลื่อนบิตของค่า 10 (0000 1010) ไปทางซ้าย 2 บิต ได้ผลลัพธ์ 40 (0010
1000) หรือ (10*2)*2 = 40 และ >> ค่าที่ได้จะเหมือนกับ การหาร 2 แล้วไม่เอาเศษ กับค่าที่ทาการเลื่อนบิตตามจานวน
บิตที่ตอ้ งการ ตามโปรแกรม คือ 10 >> 2 คือ เลื่อนบิตของค่า 10 (0000 1010) ไปทางขวา 2 บิต ได้ผลลัพธ์ 2 (0000
0010) หรือ (10 / 2) / 2 = 2 ไม่เอาเศษ (2.5)
11. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ พร้อมอธิบายการทางานของโปรแกรม
#include<stdio.h>
main( ) {
int output, intValue;
intValue = 3;
output = (intValue < 10) ? 15 : 5;
printf(“Print output data %d \n”, output);
intValue = 10;
output = (intValue < 10) ? 15 : 5;
printf(“Print output data %d \n”, output);
}

ผลจากโปรแกรม
Print output data 15
Print output data 5

อธิบายโปรแกรม
โปรแกรมอธิบายการทางานของ Conditional Operator ในส่วนของการแสดงค่าในกรณีของ condition ที่ตอ้ งการ
ตรวจสอบเป็น จริง หรือ เท็จ ว่าจะแสดงผลอย่างไร

12. จงหาผลลัพธ์ของโปรแกรมต่อไปนี้ และเขียนลาดับการทางานของตัวดาเนินการ


#include<stdio.h>
main( ) {
int X;
X = 4 + 2 * 3;
printf(“4 + 2 * 3 = %d\n”, X);
X = 15 + 7 - 5;
printf(“15 + 7 - 5 = %d\n”, X);
X = 3 + 15 % 7;
printf(“3 + 15 % 7 = %d\n”, X);
X = 45 + 32 % 27 – 32 / 4;
printf(“45 + 32 % 27 – 32 / 4 = %d\n”, X);
}
ผลจากโปรแกรม
จากโปรแกรมที่ RUN มีคา่ ที่ไม่แสดงผล ให้นิสติ เปลี่ยน % ในคาสั่ง printf เป็น
printf(“3 + 15 % 7 = %d\n”, X); เปลี่ยนเป็น printf(“3 + 15 MOD 7 = %d\n”, X);
printf(“45 + 32 % 27 – 32 / 4 = %d\n”, X); เปลี่ยนเป็น printf(“45 + 32 MOD 27 – 32 / 4 = %d\n”, X);

ลาดับการทางานของตัวดาเนินการ
1. 4 + 2 * 3 = 4 + (2 * 3)
2. 15 + 7 – 5 = (15 + 7) – 5
3. 3 + 15 % 7 = 3 + (15 % 7)
4. 45 + 32 % 27 – 32 / 4 = (45 + (32 % 27)) – (32 / 4)
เฉลยแบบฝึกหัด : คาสั่งควบคุมการทางานแบบเงื่อนไข

คาสั่งเงื่อนไข : if
1. จงรับค่าจานวนเต็ม แล้วตรวจสอบว่าค่าที่รับเข้ามานั้นเป็นค่าบวกหรือค่าลบ จากนั้นให้แสดงผลออกทาง
หน้าจอ โดยค่าที่แสดงผลนั้นจะต้องเป็นค่าบวกเท่านั้น (ใช้คาสั่ง if เท่านั้น)
#include<stdio.h>
int num;
main()
{
printf("Enter Number : ");
scanf("%d", &num);
if(num < 0)
num = num * (-1);
printf("This number is : %d", num);
}

คาสั่งเงื่อนไข : if-else
1. จงรับค่าจานวนเต็ม แล้วตรวจสอบว่าค่าที่รับเข้ามานั้นเป็นค่าบวกหรือค่าลบ จากนั้นให้แสดงผลออกทาง
หน้าจอ โดยค่าที่แสดงผลนั้นจะต้องเป็นค่าบวกเท่านั้น (ใช้คาสั่ง if-else)
#include<stdio.h>
int num;
main() {
printf("Enter Number : ");
scanf("%d", &num);
if(num < 0) {
num = num * (-1);
printf("This number is : %d", num);
}
else
printf("This number is : %d", num);
}
2. จงสร้างโปรแกรมคานวณอายุ โดยการรับค่าให้รับค่าเป็นปี ค.ศ. ที่เกิด ถ้าค่าที่ได้รับเป็นปี พ.ศ. (มากกว่าปี
ค.ศ.ปัจจุบัน) ให้แสดง Please insert in C.E. format แต่ถ้าเป็นปี ค.ศ ให้แสดงค่าอายุท่คี านวณได้ และทุกครั้ง
ที่มีการแสดงผลให้มีประโยค Goodbye! ด้วยทุกครั้ง

#include<stdio.h>
int year;
main(void) {
printf("What year was you borned? ");
scanf("%d", &year);
if(year>2012)
printf("Please insert in C.E. format\n");
else {
year = 2012-year;
printf("You are %d years old\n", year);
}
printf("Goodbye!");
}

3. จงสร้างโปรแกรมรับตัวเลขจานวนเต็ม 2 จานวน และหาผลหาร โดยทาการตรวจสอบตัวหารว่ามีค่าเป็น 0


หรือไม่ ถ้าเป็นให้แจ้งว่าหาผลหารไม่ได้
#include<stdio.h>
void main() {
float num1, num2;
printf("Enter dividend : ");
scanf("%f", &num1);
printf("Enter divisor : ");
scanf("%f", &num2);
if(num2 == 0)
printf("\nDivisor cannot be zero.");
else
printf("\nAnswer = %.2f", num1/num2);
}
4. จงสร้างตัวเลือกเพื่อคานวณหา พื้นที่วงกลม หรือ เส้นรอบวงของวงกลม และหาผลลัพธ์ที่เลือกคานวณ โดย
โปรแกรมต้องแสดงผล ดังนี้ (ใช้คำสั่ง if-else)
1. Area of the circle
2. Circumference of the circle
Enter your choice 1 or 2 :

#include<stdio.h>
main() {
int choice;
float radius, area, circum;
printf("Please select choice\n");
printf("1. Area of the circle\n");
printf("2. Circumference of the circle\n");
printf("Enter your choice 1 or 2 : ");
scanf("%d”, &choice);
if (choice == 1) {
printf("Enter Radius : ");
scanf("%f", &radius);.
area = 3.14 * radius * radius;
printf("\nArea of the circle = %.2f", area);
}
else {
printf("Enter Radius : ");
scanf("%f", &radius);.
circum = 2 * 3.14 * radius;
printf("\nCircumference of the circle = %.2f", circum);
}
}
คาสั่งเงื่อนไข : if-else if
1. จงสร้างโปรแกรมแสดงเกรด ให้รับข้อมูลเกรดจากแป้นพิมพ์ โดยกาหนดช่วงคะแนน ดังนี้ เกรด A มีช่วง
คะแนน 80-100, B+ มีช่วงคะแนน 75-79, B มีช่วงคะแนน 70 – 74, C+ มีช่วงคะแนน 65 – 69, C มีช่วง
คะแนน 60 – 64, D+ มีช่วงคะแนน 55 – 59 , D มีช่วงคะแนน 54 – 50 และ F มีช่วงคะแนนต่ากว่า 50

#include <stdio.h>
float point;
void main()
{
printf("Enter your point : ");
scanf("%f",&point);
if((point<=100) && (point>=80))
printf("Grade A\n");
else if ((point<80) && (point>=75))
printf("Grade B+\n");
else if ((point<75) && (point>=70))
printf("Grade B\n");
else if ((point<70) && (point>=65))
printf("Grade C+\n");
else if ((point<65) && (point>=60))
printf("Grade C\n");
else if ((point<60) && (point>55))
printf("Grade D+\n");
else if ((point<55) && (point>=50))
printf("Grade D\n");
else
printf("Grade F\n");
printf("See you again! Next Course");
}
2. จงสร้างโปรแกรมหาค่าที่มากที่สุดจากตัวเลข 3 จานวน
#include <stdio.h>
int a,b,c;
void main()
{
printf("Enter Number of A : ");
scanf("%d",&a);
printf("Enter Number of B : ");
scanf("%d",&b);
printf("Enter Number of C : ");
scanf("%d",&c);
if((a>b) && (a>c))
printf("Maximun fo 3 numbers is %d",a);
else if ((b>a) && (b>c))
printf("Maximun fo 3 numbers is %d",b);
else
printf("Maximun fo 3 numbers is %d",c);
}

3. จงเขียนผังงานหาค่าดัชนีมวลกายและแสดงผล “You Fat.”, “You OK.”, “You thin.”


#include <stdio.h>
void main()
{
float weight, height, BMI;
printf("Enter your weight in kg. : ");
scanf("%d",&weight);
printf("Enter your height in meter : ");
scanf("%d",&height);
BMI = weight/(height*height);
if(BMI >= 25)
printf("You’re fat.");
else if ((BMI < 25) && (BMI >= 18.5))
printf("You’re OK.");
else
printf("You’re thin.");
}
4. ร้านค้าแห่งหนึ่งลดราคาสินค้าให้แก่ลูกค้าที่มาซื้อของ ดังนี้
ถ้าซื้อสินค้า > 5,000 บาท ลดราคาให้ 5% ของราคาจ่ายจริง
ถ้าซื้อสินค้า 1,000 – 5,000 บาท ลดราคาให้ 3% ของราคาจ่ายจริง
ถ้าซื้อสินค้า > 100 บาท ลดราคาให้ 1% ของราคาจ่ายจริง
กาหนดการแสดงผลดังนี้
ราคาสินค้า : xxxx
ส่วนลด : xxxx
ราคาสินค้าหลังจากหักส่วนลดแล้ว : xxxx

#include <stdio.h>
void main() {
float price;
printf("Enter price : ");
scanf("%f",&price);
if(price >= 5000){
printf("Price : %.2f\n",price);
printf("Discount : %.2f\n",price*5/100);
printf("Total : %.2f\n",price*0.95);
}
else if ((price >= 1000) && (price < 5000)) {
printf("Price : %.2f\n",price);
printf("Discount : %.2f\n",price*3/100);
printf("Total : %.2f\n",price*0.97);
}
else if ((price > 100) && (price < 1000)) {
printf("Price : %.2f\n",price);
printf("Discount : %.2f\n",price*1/100);
printf("Total : %.2f\n",price*0.99);
}
else {
printf("Price : %.2f\n",price);
printf("Discount : 0\n");
printf("Total : %.2f\n",price);
}
}
คาสั่งเงื่อนไข : if ซ้อน if
1. จงสร้างโปรแกรมที่สามารถตรวจสอบว่าจานวนนั้นมี 3 เป็นตัวร่วมและมี 5 เป็นตัวร่วม และเป็นจานวนที่มี
ค่าไม่เกิน 100 (ใช้คำสั่ง if ซ้อน if)
#include <stdio.h>
void main()
{
int num;
printf("Enter Number : ");
scanf("%d",&num);
if(num < 100){
if(num % 3 == 0) {
if(num % 5 == 0)
printf("This Number can divided by 3 and 5.");
else
printf("Cann't divided by 5.");
}
else
printf("Cann't divided by 3.");
}
else
printf("Large number.");
}
คาสั่งเงื่อนไข : switch
1. จงสร้างโปรแกรมคานวนตัวเลข บวก ลบ คูณ หาร และยกกาลัง (ใช้คำสั่ง switch)
#include<stdio.h>
#include<math.h>
int c;
main() {
double x,y;
printf("Please select choice\n");
printf("*****************\n");
printf("1.Plus(+) #\n");
printf("2.Minus(-) #\n");
printf("3.Multiply(*) #\n");
printf("4.Divide(/) #\n");
printf("5.Power #\n");
printf("*****************\n");
printf("Select : ");
scanf("%d",&c);
printf("Please enter X : ");
scanf("%lf",&x);
printf("Please enter Y : ");
scanf("%lf",&y);
switch(c) {
case 1 : printf("%.2lf + %.2lf = %.2lf",x,y,x+y);
break;
case 2 : printf("%.2lf - %.2lf = %.2lf",x,y,x-y);
break;
case 3 : printf("%.2lf * %.2lf = %.2lf",x,y,x*y);
break;
case 4 : printf("%.2lf / %.2lf = %.2lf",x,y,x/y);
break;
case 5 : printf("%.2lf ^ %.2lf = %.2lf",x,y,pow(x,y));
break;
default : printf("Please select 1-5 only");
break;
}
}
2. จงเขียนผังงานแสดงเดือนและจานวนวันที่ป้อนเข้าไป กาหนดให้ใช้ตัวเลข 1-12 (ใช้คำสั่ง
switch)

#include<stdio.h>
int month;
main() {
printf("Please Enter Month in digit : ");
scanf("%d",&month);
switch(month) {
case 1 : printf("January has 31 days");
break;
case 2 : printf("February has 28 or 29 days");
break;
case 3 : printf("March has 31 days");
break;
case 4 : printf("April has 30 days");
break;
case 5 : printf("May has 31 days");
break;
case 6 : printf("June has 30 days");
break;
case 7 : printf("July has 31 days");
break;
case 8 : printf("August has 31 days");
break;
case 9 : printf("September has 30 days");
break;
case 10 : printf("October has 31 days");
break;
case 11 : printf("November has 30 days");
break;
case 12 : printf("December has 31 days");
break;
default : printf("Please enter 1-12 only");
break;
}
}
คาสั่งควบคุมการทางานแบบทาซ้า : while
1. จงสร้างโปรแกรมหาผลรวมของจานวนที่ป้อนเข้าไปจนถึง 0 เช่น ป้อนค่า 10 โปรแกรมจะหา
ผลบวกค่า 0-10 (0+1+2+3+4+5+6+7+8+9+10) เป็นต้น แล้วแสดงผลออกทางหน้าจอ

#include <stdio.h>
void main() {
int num, sum = 0;
printf("Enter Number : ");
scanf("%d", &num);
while(num > 0) {
sum = sum + num;
printf("%d + ", num);
num--;
}
printf(“0 = %d”, sum);
}

2. จงสร้างโปรแกรมแสดงเลขคู่ตั้งแต่ 2-200
#include<stdio.h>
main() {
int x = 2;
while(x<=200) {
if(x%2 == 0) {
printf("%d\t",x);
x++;
}
else
x++;
}
}
หรือ
#include<stdio.h>
void main() {
int x = 2;
printf("Show number form 2 to 200\n");
while(x<=200) {
printf("%d ",x);
x += 2;
}
}
คาสั่งควบคุมการทางานแบบทาซ้า : do-while
จงสร้างโปรแกรมรับค่าตัวเลขโดยให้ทาการตรวจสอบว่าตัวเลขที่ป้อนนั้นตรงกับที่โปรแกรม
กาหนดหรือไม่ โดยใช้ do-while ถ้ารับค่าตัวเลขถูกต้องให้แสดง “Thank you”

#include<stdio.h>
#include<math.h>
int choice;
main() {
double x,y;
printf("Please select choice\n");
printf("*****************\n");
printf("1.Plus(+) #\n");
printf("2.Minus(-) #\n");
printf("3.Multiply(*) #\n");
printf("4.Divide(/) #\n");
printf("5.Power #\n");
printf("*****************\n");
do {
printf("Select : ");
scanf("%d",&choice);
}while(choice < 1 || choice > 5);
printf("Thank you");
}
คาสั่งควบคุมการทางานแบบทาซ้า : for
1. จงสร้างโปรแกรมสูตรคูณโดยสามารถรับแม่สูตรคูณได้ (ใช้คำสั่ง for)
#include<stdio.h>
main()
{
int i,j;
printf("Enter Multiplication Table (Digit): ");
scanf("%d", &i);
for(j=1; j<=12; j++)
{
printf("%d * %d = %d\n",i,j,i*j);
}
}

2. จงสร้างโปรแกรมหาตัวเลข 2 จานวนที่บวกกันแล้วได้ 45 ทั้งหมด (ใช้ Multiple Expression)


#include<stdio.h>
main() {
int i,j;
for(i=0,j=45;i<=45,j>=0;i++,j--)
printf("%d + %d = %d\n",i,j,i+j);
}
คาสั่งควบคุมการทางานแบบทาซ้า : Nested for
1.

#include<stdio.h>
void main() {
int i,j;
for(i=12;i>=1;i--) {
for(j=i;j>=1;j--)
putchar('*');
printf("\n");
}
}
2. จงสร้างโปรแกรมหาจานวนเฉพาะ (Prime Number) ในช่วง 1-100
#include<stdio.h>
void main() {
int i,j,k;
for(i=1;i<=100;i++) {
k=0;
for(j=2;j<=i;j++) {
if(i%j==0)
k++;
}
if(k==1)
printf("%d\t",i);
}
}
3. จงสร้างโปรแกรมแสดงสูตรคูณแม่ 2-12 โดยให้แสดงทีละแม่
#include<stdio.h>
#include<conio.h>
main() {
int i,j;
for(i=2;i<=12;i++) {
clrscr();
for(j=1;j<=12;j++) {
printf("%d x %d\t=\t%d\n",i,j,i*j);
}
getch();
}
}

4. What’s program?
คาอธิบายโปรแกรม
คือ โปรแกรมรับตัวเลขชนิด float จากคีย์บอร์ด พร้อมนับจานวนข้อมูลและหาผลรวม โดยผู้ใช้สามารถ
กาหนดการป้อนข้อมูลได้ และเมื่อข้อมูลครบตามที่ผใู้ ช้ต้องการโปรแกรมจะคานวณค่าเฉลี่ยของตัวเลขที่
ป้อนพร้อมแสดงผล

5. What does for( ; ; ) mean?...


คือ for(;;) เป็น Loop ชนิดที่มไิ ด้กาหนดรอบการทางาน มีลักษณะการทางานแบบไม่รู้จบ ซึ่งหากใช้ Loop
รูปแบบนีใ้ นโปรแกรม ต้องระมัดระวังเนื่องจากเป็น Loop ที่ทางานแบบไม่รู้จบควรมีเงื่อนไขภายในลูปที่
สามารถสั่งให้หลุดออกจาก Loop ได้

You might also like