Professional Documents
Culture Documents
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);
}
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
ผลจากโปรแกรม
อธิบายโปรแกรม
โปรแกรมทดสอบการเปลี่ยนชนิดของตัวแปรที่มีการดาเนินการของตัวแปรคนละชนิดกัน คือ
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 ที่ตอ้ งการ
ตรวจสอบเป็น จริง หรือ เท็จ ว่าจะแสดงผลอย่างไร
ลาดับการทางานของตัวดาเนินการ
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!");
}
#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);
}
#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);
}
}
#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 จากคีย์บอร์ด พร้อมนับจานวนข้อมูลและหาผลรวม โดยผู้ใช้สามารถ
กาหนดการป้อนข้อมูลได้ และเมื่อข้อมูลครบตามที่ผใู้ ช้ต้องการโปรแกรมจะคานวณค่าเฉลี่ยของตัวเลขที่
ป้อนพร้อมแสดงผล