วันพุธที่ 1 ตุลาคม พ.ศ. 2551

การเขียนโปรแกรมภาษา C เมื่อสัปดาห์ที่แล้วกั๊บ

การเขียนโปรแกรมภาษา C เมื่อสัปดาห์ที่แล้วกั๊บ

ข้อที่ 1 (rep20.CPP)

วิธีทำ

#include
void main()
{
int n,i;
clrscr();
printf("num:"); scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d\t",i);
}
getch();
}
การเขียนโปรแกรมภาษา C เมื่อสัปดาห์ที่แล้ว
ข้อที่ 2 (repeven.CPP)

วิธีทำ

#include
void main()
{
int n,i;
clrscr();
printf("num=:"); scanf("%d",&n);
for (i=2;i<=n; i+=2){printf("%d\t",i);
}
printf("\n-----------------end of file--------------------");
getch();
}

งานสมัยก่อนโน้น

1. พัฒนาโปรแกรมเครื่องคิดเลข โดยให้ผู้ใช้ป้อนตัวเลข 2 จำนวน และเลือกเครื่องหมาย แล้วทำการแสดงผลลัพธ์ออกทางจอภาพ ดังตัวอย่างจอภาพ

วิธีทำ

#include
void main()
{
char op;
float a,b;
clrscr();
printf("Input First Number : "); scanf("%f",&a);
printf("\nInput Operator(+,-,*,/) : "); scanf("%s",&op);
printf("\nInput Second Number : "); scanf("%f",&b);
switch(op)
{
case '+': printf("\n%0.2f + %0.2f = %0.2f",a,b,a+b); break;
case '-': printf("\n%0.2f - %0.2f = %0.2f",a,b,a-b); break;
case '*': printf("\n%0.2f * %0.2f = %0.2f",a,b,a*b); break;
case '/': printf("\n%0.2f / %0.2f= %0.2f",a,b,a/b); break;
default: printf("\nPlease seleec choice (+,-,*,/)");
}
getch();
}
2. ให้เขียนโปรแกรม สำหรับคำนวณน้ำหนักมาตรฐาน โดยให้ป้อนข้อมูล ส่วนสูง น้ำหนัก เพศ[M , F]

วิธีทำ

include#
void main()
{
char fm;float h,w;
clrscr();
printf("Your Male(m) or Female(f) : "); scanf("%c",&fm);
printf("\nYour Weight : "); scanf("%f",&w);
printf("\nYour Hight : "); scanf("%f",&h);
switch(fm)
{
case 'm': if(h-100==w) printf("\nYou Standardweight ");
case 'f': if(h-110==w) printf("\nYou Standard weight ");
else if(h-110) printf("\nYour weight > Standard : %0.2fkg",w-(h-110));
else if(h-110>w) printf("\nYour weight less Than Standard : %0.2fkg",(h-110)-w);break;
getch();
}
3. ให้เขียนโปรแกรมรับตัวอักษรภาษาอังกฤษมาและบอกว่าเป็นสระ หรือ พยัญชนะ

วิธีทำ

#include(stuio.h)
void main()
{
char az;
clrscr();
printf("Please your input english word A-Z : ");
scanf("%c",&az);
switch(az);
{
case 'a': printf(" a is article"); break;
case 'e': printf(" e is article"); break;
case 'i': printf(" i is article"); break;
case 'o': printf(" o is article"); break;
case 'u': printf(" u is article"); break;
default : printf("%c is consonant",az);
}
getch();
}

งานสมัยโน้นนนนน

4. ให้เขียนโปรแกรมเพื่อรับจำนวนเงินบาท และบอกว่าใช้ธนบัตรประเภทใดเป็นจำนวนเท่าใด และต้องใช้เหรียญประเภทใด จำนวนเท่าใด

วิธีทำ

include(stuio.h)#
void main()
{
int mn;
clrscr();
printf("Input total money : ");
scanf("%i",&mn);
{
if(mn>=1000) printf("1000 bank :%i",mn/1000); mn=mn%1000;
if(mn>=500) printf("\n500 bank :%i",mn/500); mn=mn%500;
if(mn>=100) printf("\n100 bank :%i",mn/100); mn=mn%100;
if(mn>=50) printf("\n50 bank :%i",mn/50); mn=mn%50;
if(mn>=20) printf("\n20 bank :%i",mn/20); mn=mn%20;
if(mn>=10) printf("\n10 coin :%i",mn/10); mn=mn%10
if(mn>=5) printf("\n5 coin :%i",mn/5); mn=mn%5;
if(mn>=1) printf("\n1 coin :%i",mn);
getch();
}