Contents
- 1Control statements
- 1.1คำนวณผลรวมของ 1-10
- 1.2รับตัวเลขจากผู้ใช้ แล้วตรวจสอบว่าเป็นเลขคู่ (แสดงคำว่า Even ) หรือเลขคี่ (แสดงคำว่า Odd)
- 1.3ฟังก์ชันหาค่าเฉลี่ยของเลขจำนวนเต็ม 2 จำนวน (คืนค่าเป็นทศนิยม)
- 1.4คำนวณหาพื้นที่ของสามเหลี่ยม โดยรับค่าความสูงของสามเหลี่ยม (Height) และค่าของฐานสามเหลี่ยม (Base)
- 1.5คำนวณหาพื้นที่ของวงกลม โดยรับค่ารัศมี (Radius) และกำหนดให้ค่าพายเท่ากับ 3.14
- 1.6โปรแกรมรับตัวเลขทางแป้นพิมพ์เป็นเลขฐานสิบ (0-7) จากนั้นแสดงผลเป็นเลขฐานสองแบบสามหลัก
- 1.7พิมพ์แม่สูตรคูณ แม่ 2 ถึง 25
- 1.8ตัดเกรด A - F ด้วยคำสั่ง if
- 1.9ตัดเกรด A - F ด้วยคำสั่ง switch ... case
- 1.10รับวันที่ (1-30) ของเดือนกันยายน พ.ศ. 2558 แล้วแสดงวัน (อาทิตย์ จันทร์ อังคาร พุธ พฤหัส ศุกร์ เสาร์)
- 1.11Display Character from A to Z Using Loop
- 1.12checking is Prime or not
- 1.13Prime Numbers Between two Interval number
- 1.14Fibonacci series
- 1.15
- 1.16Floyd's Triangle
- 1.17
- 1.18
- 1.19Display all factors of a number
- 2
- 3Recursive
- 3.1ผลรวม
- 3.2Factorial
- 3.3Fibonacci at n (terms)
- 4Other
- 4.1Display its own source code
Control statements
คำนวณผลรวมของ 1-10
รับตัวเลขจากผู้ใช้ แล้วตรวจสอบว่าเป็นเลขคู่ (แสดงคำว่า Even ) หรือเลขคี่ (แสดงคำว่า Odd)
ฟังก์ชันหาค่าเฉลี่ยของเลขจำนวนเต็ม 2 จำนวน (คืนค่าเป็นทศนิยม)
คำนวณหาพื้นที่ของสามเหลี่ยม โดยรับค่าความสูงของสามเหลี่ยม (Height) และค่าของฐานสามเหลี่ยม (Base)
คำนวณหาพื้นที่ของวงกลม โดยรับค่ารัศมี (Radius) และกำหนดให้ค่าพายเท่ากับ 3.14
โปรแกรมรับตัวเลขทางแป้นพิมพ์เป็นเลขฐานสิบ (0-7) จากนั้นแสดงผลเป็นเลขฐานสองแบบสามหลัก
พิมพ์แม่สูตรคูณ แม่ 2 ถึง 25
ตัดเกรด A – F ด้วยคำสั่ง if
ตัดเกรด A – F ด้วยคำสั่ง switch … case
รับวันที่ (1-30) ของเดือนกันยายน พ.ศ. 2558 แล้วแสดงวัน (อาทิตย์ จันทร์ อังคาร พุธ พฤหัส ศุกร์ เสาร์)
Display Character from A to Z Using Loop
checking is Prime or not
Prime Numbers Between two Interval number
Fibonacci series
Floyd’s Triangle
Display all factors of a number
Recursive
ผลรวม
Factorial
Fibonacci at n (terms)
Other
Display its own source code
Control statements
คำนวณผลรวมของ 1-10
1 2 3 4 5 6 7 8 |
#include <stdio.h> void main() { int i; int sum = 0; for (i = 1; i < 11; i++) sum += i; printf("Total = %d\n", sum); } |
รับตัวเลขจากผู้ใช้ แล้วตรวจสอบว่าเป็นเลขคู่ (แสดงคำว่า Even ) หรือเลขคี่ (แสดงคำว่า Odd)
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> void main() { int num; printf("Enter number : "); scanf("%d", &num); if (num % 2 == 0) printf(" Even\n"); else printf(" Odd\n"); } |
ฟังก์ชันหาค่าเฉลี่ยของเลขจำนวนเต็ม 2 จำนวน (คืนค่าเป็นทศนิยม)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> float Average(int a, int b) { return (float) (a + b) / 2; } void main() { int num1; int num2; float ans; printf("Enter first number : "); scanf("%d", &num1); printf("Enter second number : "); scanf("%d", &num2); ans = Average(num1, num2); printf("Average = %f\n", ans); } |
คำนวณหาพื้นที่ของสามเหลี่ยม โดยรับค่าความสูงของสามเหลี่ยม (Height) และค่าของฐานสามเหลี่ยม (Base)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> float AreaTriangle(float h, float b) { return (h * b) / 2; } void main() { float h; float b; float ans; printf("Enter height : "); scanf("%f", &h); printf("Enter base : "); scanf("%f", &b); ans = AreaTriangle(h, b); printf("Area = %f\n", ans); } |
คำนวณหาพื้นที่ของวงกลม โดยรับค่ารัศมี (Radius) และกำหนดให้ค่าพายเท่ากับ 3.14
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> float pi = 3.14; float AreaCircle(float r) { return pi * r * r; } void main() { float r; float ans; printf("Enter radius : "); scanf("%f", &r); ans = AreaCircle(r); printf("Area = %f\n", ans); } |
โปรแกรมรับตัวเลขทางแป้นพิมพ์เป็นเลขฐานสิบ (0-7) จากนั้นแสดงผลเป็นเลขฐานสองแบบสามหลัก
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> void main() { int num; printf("Enter (0-7) : "); scanf("%d", &num); int l3 = (num % 8) / 4; int l2 = (num % 4) / 2; int l1 = num % 2; printf("%d%d%d\n", l3, l2, l1); } |
พิมพ์แม่สูตรคูณ แม่ 2 ถึง 25
1 2 3 4 5 6 7 8 9 10 |
#include <stdio.h> void main() { int i; int num; printf("Enter (2-25) : "); scanf("%d", &num); for (i = 1; i <= 12; i++) { printf("%d x %d = %d\n", num, i, num * i); } } |
ตัดเกรด A – F ด้วยคำสั่ง if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> void main() { int i; int num; printf("Enter score : "); scanf("%d", &num); if (num < 50) printf("Grade F\n"); else if (num < 60) printf("Grade D\n"); else if (num < 70) printf("Grade C\n"); else if (num < 80) printf("Grade B\n"); else if (num <= 100) printf("Grade A\n"); } |
ตัดเกรด A – F ด้วยคำสั่ง switch … case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <stdio.h> void main() { int num; printf("Enter score : "); scanf("%d", &num); switch (num / 10) { case 10: case 9: case 8: printf("Grade A\n"); break; case 7: printf("Grade B\n"); break; case 6: printf("Grade C\n"); break; case 5: printf("Grade D\n"); break; default: printf("Grade F\n"); break; } } |
รับวันที่ (1-30) ของเดือนกันยายน พ.ศ. 2558 แล้วแสดงวัน (อาทิตย์ จันทร์ อังคาร พุธ พฤหัส ศุกร์ เสาร์)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> void main() { int num; printf("Enter day (1-30) : "); scanf("%d", &num); switch (num % 7) { case 1: printf("Tuesday\n"); break; case 2: printf("Wednesday\n"); break; case 3: printf("Thursday\n"); break; case 4: printf("Friday\n"); break; case 5: printf("Saturday\n"); break; case 6: printf("Sunday\n"); break; case 0: printf("Monday\n"); break; } } |
Display Character from A to Z Using Loop
1 2 3 4 5 6 7 |
#include <stdio.h> void main() { char c; for (c = 'A'; c <= 'Z'; ++c) printf("%c ", c); printf("\n"); } |
checking is Prime or not
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int isPrime(int num) { int flag = 0; int i; for (i = 2; i <= num / 2; ++i) { if (num % i == 0) { flag = 1; break; } } return flag; } void main() { int n; printf("Enter numbers : "); scanf("%d", &n); if (isPrime(n) == 0) printf("%d is Prime\n", n); else printf("%d is not Prime\n", n); } |
Prime Numbers Between two Interval number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> int isPrime(int num) { int flag = 0; int i; for (i = 2; i <= num / 2; ++i) { if (num % i == 0) { flag = 1; break; } } return flag; } void main() { int n1, n2, i, flag; printf("Enter two numbers (intervals): "); scanf("%d %d", &n1, &n2); printf("Prime numbers between %d and %d are: ", n1, n2); for (i = n1 + 1; i < n2; ++i) { flag = isPrime(i); if (flag == 0) printf("%d ", i); } printf("\n"); } |
Fibonacci series
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> void main() { int n, count; int t1 = 0, t2 = 1; int next = 0; printf("Enter number of terms: "); scanf("%d", &n); printf("Fibonacci Series: %d+%d", t1, t2); count = 2; while (count < n) { next = t1 + t2; printf("+%d", next); t1 = t2; t2 = next; count++; } printf("\n"); } |
Floyd’s Triangle
1
2 3
4 5 6
7 8 9 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> void main() { int i, j, k = 1; int rows; printf("Enter number of rows: "); scanf("%d", &rows); printf("Floyd's Triangle : \n"); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++, k++) { printf("%d ", k); } printf("\n"); } } |
Display all factors of a number
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <stdio.h> void main() { int n, i; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factors of %d are: ", n); for (i = 1; i <= n; ++i) { if (n % i == 0) printf("%d ", i); } printf("\n"); } |
Recursive
ผลรวม
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> int sum(int n) { if (n == 0) return 0; else return n + sum(n - 1); } void main() { int n; printf("Enter number : "); scanf("%d", &n); printf("sum of %d is %d\n", n, sum(n)); } |
Factorial
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> int factorial(int i) { if (i <= 1) { return 1; } return i * factorial(i - 1); } void main() { int n; printf("Enter number : "); scanf("%d", &n); printf("Factorial of %d is %d\n", n, factorial(n)); } |
Fibonacci at n (terms)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int fibonacci(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } return fibonacci(n - 1) + fibonacci(n - 2); } void main() { int n; int ans; printf("Enter number of terms: "); scanf("%d", &n); ans = fibonacci(n); printf("Fibonacci at %d: %d\n", n, ans); } |
Other
Display its own source code
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> void main() { FILE *fp; char c; fp = fopen(__FILE__, "r"); do { c = getc(fp); putchar(c); } while (c != EOF); fclose(fp); } |