More Related Content Similar to นางสาวกัญญารัตน์ คิดในทางดี (20) นางสาวกัญญารัตน์ คิดในทางดี4. 3.แล้วพิมพ์โค้ดในลักษณะนี้ค่ะ
#include "stdio.h" void main() {
int num1,num2,sum;
printf("please insert number1:");
scanf("%d",&num1);
printf("please insert number2:");
scanf("%d",&num2); sum =
num1 + num2; printf("Sum of
%d + %d =
%d",num1,num2,sum);
getch();
return 0; }
4.แล้วกด compile&run หรือกดแป้นพิมพ์F11
5. ใส่ตัวเลข 5 และ 6 จะได้ผลลพธ์ตามรูป
Week3_trapezoid
1. ไปที่โปรแกรม Dev C++
2. กด Ctrl+n เพื่อเพิ่มหน้าจอการทางาน
3. แล้วพิมพ์โค้ดในลักษณะนี้
การหาพื้นที่สี่เหลี่ยมคางหมูมีสูตรดังนี้
ผลบวกของด้านคู่ขนาน สูง
6. กาหนด w1,w2 = ผลบวกของ
ด้านคู่ขนาน
h = ความสูง
area = พื้นที่ทั้งหมด 0.5×(w1+w2) ×h
*1/2=0.5
#include <stdio.h> void
main()
{
float w1,w2,h,area; *float ใช้กับทศนิยม(7ตาแหน่ง)
printf("Please insret side1:"); scanf("%f",&w1); printf("Please insret side2:");
scanf("%f",&w2); printf("Please insert to Height:"); scanf("%f",&h); area = 0.5 *
(w1 + w2) * h; printf("Area of tranpezoid side1:%f side:2%f and Height:%f is
Area:%f",w1,w2,h,area); getch(); return 0;
}
4. 4.แล้วกด compile&run หรือกดแป้นพิมพ์F11
8. Week4_counting char
เป็นการนับตัวอักษรตามที่ได้พิมพ์ลงไป โดยจะมี
#include "string.h" , char name[30]; และ strlen(name)); เป็นตัวบ่งบอก
1.เปิดโปรแกรมDevC++ แลว้Ctrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
2. แล้วพิมพ์โค้ดในลักษณะนี้
#include "stdio.h"
#include "string.h"
ข้อความ void main()
{
char name[30]; *char ใช้กับตัวอักษร 1 ตัว
printf("Please insert your name-surname:"); gets(name); printf("%s has %d character"
,name,strlen(name)); getch(); return 0;
}
ฟังก์ชัน จัดการข้อความรวม
9. 3.แล้วกด compile&run หรือกดแป้นพิมพ์ F11 ให้ใส่ชื่อหรือประโยคที่ต้องการนับ
ตัวอักษรษรลงไปแล้วกด Enter ก็จะแสดงผลดังรูป
Week4_triangle
1.เข้าโปรแกรมDevC++ แลว้พิมพ์Ctrl+nเพื่อเพิ่มโปรเจคใหม่
2.เมื่อได้หน้าโปรเจคใหม่แล้วให้วิเคราะห์โจทย์ที่ได้มา คือ การหาพื้นที่สามเหลี่ยม โดยในการหาพื้นที่ครั้ง
นี้เราสามารถป้อนข้อมูลที่เป็นจุดทศนิยมได้ และคาตอบที่ได้จะเป็นจุดทศนิยมเช่นกัน
3.พิมพ์ภาษาซีลงไปในหน้าโปรเจคนี้
#include <stdio.h> void main ()
{
float b,h,area; * floatจะใช้กับตัวเลขที่มีการหาทศนิยม
printf("please insert Base of triangle:"); scanf("%f",&b); printf("please insert height of
10. triangle:"); scanf("%f",&h); area = 0.5 * b * h; printf("Area of triangle base:%f and
height:%f area is %.2f",b,h,area); getch(); return 0;
}
4.แล้วกด compile&run หรือกดแป้นพิมพ์ F11 จะได้ผลลัพธ์ดังรูปภาพ
ซึ่งในรูปภาพนี้ได้มีการพิมพ์ตัวเลขเพื่อคานวณหาพื้นที่แล้ว
Week5_age
เป็นการถามอายุ ถ้ามากกว่า 60 จะโชว์ข้อความ You are Oldest!!! แต่
ถ้าอายุไม่ถึง 60 จะโชว์ข้อความ You are baby face :P
1.เข้าโปรแกรมDevC++ แล้วกด Ctrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
2.แล้วพิมพ์โค้ดในลักษณะนี้
11. #include "stdio.h" void
main() { int age;
printf("How old are you:");
scanf("%d",&age); if(age
>= 60)
{
printf("You are Oldest!!!naa");
}
else
{
printf("You are baby face :Pnaa");
}
printf("Good Bye");
getch();
return 0;
}
12. 3.แล้วกด compile&run หรือกดแป้นพิมพ์ F11
Week5_char input
เป็นการบวกกนของตัวอักษรโดยใช้ค่าจากรหัส ASCII โดยในครั้งนี้เราจะ
กาหนดตัวอักษรเอง โดยการ input ข้อมูลเข้าไป
1.เข้าโปรแกรมDevC++ แล้วกด Ctrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
13. 2.แล้วพิมพ์โค้ดในลักษณะนี้
#include "stdio.h" void main() { char ch1,ch2;
printf("Please insert Character1:"); scanf("
%c",&ch1); printf("Please insert Character2:");
scanf(" %c",&ch2); printf("ch1:%d + ch2:%d =
%d",ch1,ch2,ch1+ch2); if(ch2 > ch1)
{
printf("nCh2 more than ch1");
}
printf("nGoodBye");
getch();
return 0;
}
3.แล้วกด compile&run หรือกดแป้นพิมพ์ F11
16. 3.แล้วกดcompile&run หรือกดแป้นพิมพ์ F11
Week6_Grade
เป็นการใช้ฟังก์ชัน if else เลือกอย่างใดอย่างหนึ่ง เป็นการ
คานวณหาค่าเกรดเฉลี่ยโดยได้มีการกาหนดให้
A = (score>=80)
B+ =((score >=75)&&(score <=79))
B =((score >=70)&&(score <=74))
C+ =((score >=65)&&(score <=69))
C =((score >=60)&&(score <=64))
D+ =((score >=55)&&(score <=59)) D
=((score >=50)&&(score <=54))
แต่ถ้าได้น้อยกว่าที่กาหนดให้คือ F
17. 1.เข้าโปรแกรมDevC++ แล้วกดCtrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
2.แล้วพิมพ์โค้ดในลักษณะนี้
#include "stdio.h" void main() {
int score; printf("Please insert
your score:");
scanf("%d",&score);
if(score>=80)
printf("You are Grade A"); else
if ((score >=75)&&(score <=79))
printf("You are Grade B+"); else if
((score >=70)&&(score <=74))
printf("You are Grade B"); else if
((score >=65)&&(score <=69))
printf("You are Grade C+"); else if
((score >=60)&&(score <=64))
printf("You are Grade C"); else if
((score >=55)&&(score <=59))
printf("You are Grade D+"); else if
((score >=50)&&(score <=54))
18. printf("You are Grade D"); else
printf("You are Grade F");
printf("nGoodBye"); getch();
return 0; }
3.แล้วกด compile&run หรือกดแป้นพิมพ์F11
19. Week6_switch case for
รูปแบบของ switch case ดังนี้
switch(variable)
{ case contants:
statement;
break;
case contants_2;
break;
default:statement;
}
For คือ การทา ซ้า หรือวนลูป การ
ทา ซ้าจนกว่าจะครบตัวแปรตามที่ตั้งไว้
เป็นการสั่งอาหารแบบสั่งได้หลายเมนูโดยพิมพ์ตามที่กาหนดก็จะขึ้นรายชื่ออาหารและราคา
1.เข้าโปรแกรมDevC++ แล้วกดCtrl+n เพื่อเพิ่มหน้า โปรเจคใหม่
20. 2.แล้วพิมพ์โค้ดในลักษณะนี้
#include "stdio.h" void main() { char
menu; int counter; for(counter=1 ;
counter <5 ; counter++)
{
printf("You want to Order:");
scanf(" %c",&menu); switch(menu)
{
case 'p' : printf("Papaya Salad 120n");
break; case 'c' : printf("Chicken Grill 500n");
break; case 't' : printf("Tomyam bone young
180n"); break; case 's' : printf("Sticky
rice 90n"); break;
default : printf("nTry again");
}
} printf("n------------------------
---"); getch(); return 0;
}
22. Week6_switch case
รูปแบบของ switch case ดังนี้
switch(variable)
{
case contants: statement;
break;
case contants_2; break;
default:statement;
}
ในที่นี้เป็นการให้เลือกสั่งอาหารโดยพิมพ์ตามที่กาหนดจะขึ้นเมนูและค่าอาหาร
1.เข้าโปรแกรมDevC++ แล้ว Ctrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
2.แล้วพิมพ์โค้ด
ในลักษณะนี้
#include "stdio.h"
void main() {
char menu;
printf("You want
23. to Order:");
scanf("
%c",&menu);
switch(menu)
{
case 'p' : printf("Papaya Salad 120");
break; case 'c' : printf("Chicken Grill 500");
break; case 't' : printf("Tomyam bone young
180"); break; case 's' : printf("Sticky
rice 90"); break; default :
printf("nTry again");
} printf("n------------------------
---"); getch(); return 0;
}
24. 3.แล้วกด compile&run หรือกดแป้นพิมพ์F11
Week7_sudkoon while
While เป็นการทา งานซ้าโดยตรวจสอบเงื่อนไขก่อน เป็นจริงจะ
ทา ซ้าแล้วจะวนจนเป็นเงื่อนไขเท็จ
เป็นการแสดงแม่สูตรคูณแบบพื้นฐานเริ่มต้นที่แม่2
1.เข้าโปรแกรมDevC++ แล้ว Ctrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
2. แล้วพิมพ์โค้ดใน
ลักษณะนี้
#include "stdio.h" void
main() { int
27. {
printf("%d x %d =%dn",number,counter,number * counter);
}
}
printf("nGoodBye")
; getch(); return 0;
}
3.แล้วกด compile&run หรือกดแป้นพิมพ์F11
29. 1.เข้าโปรแกรมDevC++ แล้ว Ctrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
2.แล้วพิมพ์โค้ด
ในลักษณะนี้
#include
"stdio.h" void
main() { int
a[5]; a[0] = 1;
a[1] = 2; a[2] = 3;
a[3] = 4; a[4] = 5;
printf("%d %d
%d %d
%d",a[0],a[1],a[2
],a[3],a[4]);
printf("nGoodBy
e"); getch();
return 0;
}
31. 1.เข้าโปรแกรมDevC++ แล้ว Ctrl+n เพื่อเพิ่มหน้าโปรเจคใหม่
2.แล้วพิมพ์โค้ดใน
ลักษณะนี้
#include "stdio.h"
void main() { int
table[9][9],i,j,x;
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{ x
= i-1; table[x][j-
1]= i*j; }
}
printf("* |t1t2t3t4t5t6t7t8t9n"); printf("-----------------------------------------------
---------------------------n"); for(i=0;i<9;i++)
{ printf("%d |t",i+1);
for(j=0;j<9;j++)
{
printf("%dt",table[i][j]);
}