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();
}

โปรแกรมแสดงเลขคู่

#include "stdio.h"
#include "conio.h"
void main()
{
int i=2,num;
clrscr();
printf("Input Num :");scanf("%i",&num);
while (i <= num)
{
printf("%i ",i);
i+=2;
}
getch();
}

โปรแกรมแสดงเลข 1 – 20

#include "stdio.h"
#include "conio.h"
void main()
{
int i;
clrscr();
for(i=1;i<=20;i++) 
{
printf("%i ",i);
    }
getch();
}

โปรแกรมเพื่อรับจำนวนเงินบาท

#include "stdio.h"
#include "conio.h"
void main() 
{
int money,bank1000 =0 ,bank500 =0 ,bank100 =0,bank50 =0 ,bank20=0,bank10 =0 ,bank5 =0 ,bank1 = 0;
clrscr();
printf("Money = ");scanf("%d",&money);
if (money >= 1000)
{
  bank1000 = div(money,1000);
  money = money - (bank1000 * 1000);
}
if (money >= 500)
{
  bank500 = div(money,500);
  money = money - (bank500 * 500);
}
if (money >= 100)
{
  bank100 = div(money,100);
  money = money - (bank100 * 100);
}
if (money >= 50)
{
  bank50 = div(money,50);
  money = money - (bank50 * 50);
}
    if (money >= 20)
{
  bank20 = div(money,20);
  money = money - (bank20 * 20);
}
if (money >= 10)
{
bank10 = div(money,10);
money = money - (bank10 * 10);
   }
if (money >= 5)
{
bank5 = div(money,5);
money = money - (bank5 * 5);
if (money >= 0) bank1 = money;
else
{
printf("Money Incorret Type\n");
printf("Press Any Key To Exit\n");
exit();
    }
printf(" 1000 = %d\n",bank1000);
printf("  500 = %d\n",bank500);
printf("  100 = %d\n",bank100);
printf("   50 = %d\n",bank50);
printf("   20 = %d\n",bank20);
printf("   10 = %d\n",bank10);
printf("    5 = %d\n",bank5);
printf("    1 = %d\n",bank1);
   getch();
}

โปรแกรมรับตัวอักษรภาษาอังกฤษ

#include "stdio.h"
#include "conio.h"
#include "ctype.h"
void main()
{
char engchar;
clrscr();
printf("Input English charater :");scanf("%s",&engchar);
engchar =  toupper(engchar);
if ( engchar == 'A'  ||engchar == 'E' ||engchar == 'I'  ||engchar == 'O' || engchar == 'U' ) printf ("%c Is ddddd",engchar);
 else
printf("%c In aaa",engchar);
getch();
}

โปรแกรม สำหรับคำนวณน้ำหนักมาตรฐาน

#include
#include
void main()
{
float weight,height,standard;
char gender,name[30];
    clrscr();
    printf("Input Name     : ");scanf("%s",&name);
    printf("Input Height   : ");scanf("%f",&height);
    printf("Input Weight   : ");scanf("%f",&weight);
    printf("Input Gender   : ");scanf("%s",&gender);
    printf("\n");
    printf("\n");
    printf("\n");
    if (gender =='M' || gender == 'm') standard = height - 100;
       else  standard = height - 110;
    if ( standard == weight) printf("%s Weight Is Standard",name);
       else if (standard > weight) printf("%s Weight Is Under Standard %.2f Kg",name,standard-weight);
       else  printf("%s Weight Is Over Standard %.2f Kg",name,weight-standard);
    getch();
}

โปรแกรมเครื่องคิดเลข

#include
#include
void main() 
{
int num1,num2;
float total;
char mathoperator;
clrscr();
printf("Input First Number         : ");scanf("%d",&num1);
printf("Input Second  Number       : ");scanf("%d",&num2);
printf("Input Operator [+, -, *, /]:  ");scanf("%s",&mathoperator);
  if (mathoperator =='+') total = num1+num2;
else  if (mathoperator =='-') total = num1-num2;
else  if (mathoperator =='*') total = num1*num2;
 else  if (mathoperator =='/') total = num1/num2;
  else
  {
  printf("Wrong Operator\n");
  printf("Press Any Key to Exit\n");
  getch();
  exit();
  }
printf("Output : %d %c %d = %.0f",num1,mathoperator,num2,total);
getch();
}

โปรแกรมรับคะแนนกลางภาค ปลายภาค

#include
#include
void main()
{
int mid,final,sum;
clrscr();
printf("Input Score Midterm = ");scanf("%d",&mid);
printf("Input Score Final   = ");scanf("%d",&final);
    sum = mid + final;
printf("Sum Score           = %d\n",sum);
if (sum >= 80)
{
printf("Grade               = A ");
}
else if (sum >= 70)
{
printf("Grade               = B ");
}
else if (sum >= 60)
{
printf("Grade               = C ");
}
else if (sum >= 50)
{
printf("Grade               = D ");
}
else
{
printf("Grade               = E ");
}
getch();
}

โปรแกรมรับตัวเลข 0-100 50_3.cpp

#include
#include
void main()
{
int number;
clrscr();
printf("Input number 0-100 :");scanf("%d",&number);
if (number > 50)
{
printf("Number Is  > 50 ");
}
else if (number <>
{
printf("Number Is <>);
}
else
{
printf("Number Is = 50");
}
getch();
}

โปรแกรมเพื่อรับตัวเลข 0-100

#include
#include
void main()
{
int number;
clrscr();
printf("Input number 0-100 :");scanf("%d",&number);
if (number > 50)
{
    printf("Number Is ");
}else
{
  printf("Number Is ");
}
getch();
}