10 ตุลาคม 2551

โปรแกรมคำนวณน้ำประปา

#include  "stdio.h"
#include "conio.h"
void main()
{
int end=0,unit,price,total;
char choice;
do
{
clrscr();
gotoxy(20,5);printf("Calculate Papa\n");
gotoxy(20,6);printf("1. Rate\n");
gotoxy(20,7);printf("2. Calculate\n");
gotoxy(20,8);printf("3. Exit");
switch (choice=getch())
{
case '1' :
clrscr();
                        gotoxy(20,5);printf("0 -  10 unit   2 Baht/unit\n");
gotoxy(20,6);printf("11 - 20 unit  4 Baht/unit\n");
gotoxy(20,7);printf("> 20 unit      5 Baht/unit\n");
gotoxy(20,9);printf("Press any key to continue");
getch();
end = 0;
break;
case '2' :
case2: clrscr();
                gotoxy(20,5);printf("Unit : ");scanf("%d",&unit);
if (unit > 0 && unit <= 10) price = 2;
else if (unit >=11 && unit <=20) price = 4;
else if (unit > 20) price = 5;
else goto case2;
total = unit * price;
gotoxy(20,6);printf("%d Baht/unit",unit);
gotoxy(20,7);printf("Pay %3d Baht",total);
gotoxy(20,9);printf("Press any key to continue");
getch();
end = 0;
break;
case '3' :
end = 1;
break;
// default :  goto again;
}
}
while (end == 0);
}

พัฒนาโปรแกรมเพื่อทำการแสดงผลดังตัวอย่าง (File name: star.cpp)

*
**
***
****

#include "stdio.h"
#include "conio.h"
#include "string.h"
void main()
{
int count=20,i;
char star[20] = "";
clrscr();
for(i=1;i<=count;i++)  printf("%s\n", strcat (star,"*"));
}

โปรแกรมเพื่อหาผลรวมของตัวเลขคู่ และเลขคี่ ที่อยู่ระหว่าง 0 ถึง 100

#include "stdio.h"
#include "conio.h"
void main()
{
int sum1=0,sum2=0,i;
clrscr();
for(i=1;i<=100;i++) 
if (i%2==0) sum1+=i; else sum2+=i;
printf("Odd Value: 1+3+5+7+9+11+…+99=%d\n",sum2);
printf("Even Value: 2+4+6+8+10+12+…+100=%d\n",sum1);
getch();
}