Week 1
โชว์เลขที่ห่างกันอยู่ 4 จานวน แต่ไม่เกิน 10
เฮดชาร์ทหลัก ๆ คือ
#include “stdio.h”
void main()
ไฟล์นามสกุล .h เรียกว่า Header file หรือไฟล์ส่วนหัวที่มีการประกาศรายละเอียดของคาสั่งในภาษา C
int คือการรับค่าจานวนเต็ม
เมื่อเขียนโปรแกรม
#include “stdio.h”
void main()
{
int f;
for(f=1;f <=10; f=f+4)
{
printf(“%d”,f);
}
printf(“nGoodbye”);
getch();
return0;
}
การโชว์แสดงผลข้อความทางจอภาพ
ใช้คำสั่งprintf แล้วปิดด้วย getch();
Week 2
หาปริมาตรสี่เหลี่ยม
#include <stdio.h>
void main()
{
float w1,w2,h,area;
printf("Please insert side1:");
scanf("%f",&w1);
printf("Please insert side2:");
scanf("%f",&w2);
printf("Please insert to Height:");
scanf("%f",&h);
area = 0.5 *(w1 +w2) *h;
printf("Area of trapezoid side:%f side:%f and Height:%f is Area:%f",w1,w2,h,area);
getch();
return0;
}
ใช้ float เพราะเป็นเลขทศนิยม
เมื่อคำนวณออกมำจะได้ผลดังนี้
Week 3
การนับตัวอักษร
#include "stdio.h"
#include "string.h"
void main()
{
char name[30];
printf("Please insert your name-surname:");
gets(name);
printf("%shas %d character",name,strlen(name));
getch();
return0;
}
เพิ่มเฮดชาร์ด "string.h"
"string.h กลุ่มฟังก์ชั่นที่ใช้ในการจัดการข้อความ เช่น นาสองข้อความมาต่อกัน,
ค้นหาอักขระในข้อความ, รวมข้อความ
ใช้char หรือ %s ใช้แสดงค่าที่เป็นตัวอักษร 1ตัว
เช่น
ใส่ชื่อไป Sirilak มี 7ตัวอักษร หน้าจอก็จะแสดงผลว่ามี 7 ตัวอักษร
การหาพื้นที่สามเหลี่ยม
#include <stdio.h>
void main()
{
float b,h,area;
printf("Please insert Base of triangle:");
scanf("%f",&b);
printf("Please insert height of triangle:");
scanf("%f",&h);
area = 0.5 * b* h;
printf("Areaof triangle base:%f height:%f is area:%.2f",b,h,area);
getch();
return 0;
}
0.5 คือ ½ ตามสูตรการหา พื้นที่ของสามเหลี่ยมคือ ½ x ฐาน x สูง
ใช้float เพราะเป็นเลขทศนิยม
Week 4
การประมวลผลอายุ
#include"stdio.h"
void main()
{
int age;
printf("Howold are you:");
scanf("%d",&age);
if(age>= 60)
{
printf("Youare Oldest!!!!naa");
}
else
{
printf("Youare babyface :pnaaaaaa");
}
printf("GoodBye");
getch();
return 0;
}
ใช้If else เพราะมีการประมวลผล
ถ้า if(age>= 60) คือ มีอายุมากกว่า 60 ข้อความจะแสดงว่า You are Oldest!!!!
แต่ถ้าต่ากว่า 60 จะแสดง You are babyface
เช่น
คือ มีอายุมากกว่า 60 ปี
ถ้ำมีอำยุต่ำกว่ำ60ข้อควำมก็จะแสดงYouare baby face
ใช้%dเพราะเก็บค่าตัวเลขจานวนเต็ม
Week 5
ใช้ Char บวกเลข
#include "stdio.h"
void main()
{
char ch1 = 'g';
char ch2 = 'k';
printf("ch1:%d+ ch2:%d= %d",ch1,ch2,ch1+ch2);
if(ch2 > ch1)
{
printf("nCh2more than ch1");
}
printf("nGoodbye");
getch();
return 0;
}
Week 6
การประมวลผลเกรด
#include"stdio.h"
void main()
{
int score;
printf("Pleaseinsert your score:");
scanf("%d",&score);
if(score>=80)
printf("Youare Grade A");
else if((score >=75)&&(score<=79))
printf("Youare Grade B+");
else if((score >=70)&&(score<=74))
printf("Youare Grade B");
else if((score >=65)&&(score<=69))
printf("Youare Grade C+");
else if ((score >= 60)&&(score <= 64))
printf("Youare Grade C");
else if ((score >= 55)&&(score <= 59))
printf("Youare Grade D+");
else if ((score >= 50)&&(score <= 54))
printf("Youare Grade D");
else
printf("Youare Grade F");
printf("nGoodbye");
getch();
return 0;
}
Week 7
ใช้ else if มีเงื่อนไข
ต่ากว่า50 ได้ เกรด F
Week 8
การสั่งอาหาร
#include"stdio.h"
void main()
{
char menu;
int counter;
for(counter=1; counter<5; counter++)
{
printf("Youwant to Order:");
scanf(" %c",&menu);
switch(menu)
{
case 'p':printf("Papayasalad 120");
break;
case 'c':printf("Chicken Grill 500");
break;
case 't':printf("Tomyumboneyoung 180");
break;
case 's' : printf("Stickyrice 90");
break;
default : printf("nTryagain");
}
}
printf("n--------------^_^----------------");
getch();
return 0;
}
เลือกรายการอาหาร สั่งอาหาร ใช้ switch case
ตามเงื่อนไข ถ้าต้องการ ส้มตา ให้พิมพ์ตัว P
สูตรคูณ
#include "stdio.h"
void main()
{
int counter,number,counter1;
for(counter1=1;counter1<=5;counter++)
{
printf("Please insert Number:");
scanf("%d",&number);
for(counter=1;counter<=12;counter++)
{
printf("%dx%d =%dn",number,counter,number * counter);
}
}
printf("nGoodbye");
getch();
return 0;
}
ใช้int เพราะรับค่าที่เป็นจานวนเต็ม
การใช้ dowhile ทาสูตรคูณ
กาหนดว่าใช้ แม่สูตรคูณ 2
#include "stdio.h"
void main()
{
int counter;
counter=1;
do
{
printf("2x %d= %dn",counter,2* counter);
counter++;
}while(counter<=12);
printf("nGoodbye");
getch();
return 0;
}
การใช้ whileทาสูตรคูณ
#include"stdio.h"
void main()
{
int counter;
counter=1;
while(counter<=12)
{
printf("2x %d = %dn",counter,2* counter);
counter++;
}
printf("nGoodbye");
getch();
return 0;
}
กลับตัวหนังสือใช้ array
#include "stdio.h"
#include "conio.h"
#define MAX_VALUE50
void main()
{
int i;
char word[MAX_VALUE];
printf("Enteryour word:");
gets(word);
printf("Reverse ofyourword is:");
for(i=strlen(word)-1;i>=0;i--)
{
printf("%c",word[i]);
}
getch();
return 0;
}
เพิ่มเฮดชำร์ต "conio.h"
ยกตัวอย่ำงคำ
ใช้ array ทาสูตรคูณเป็นตารางมี คอลัม
#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]);
}
}
getch ();
return 0;
}

Dev c