วันจันทร์ที่ 6 สิงหาคม พ.ศ. 2561

ตัวอย่างคำสั่ง loop do_while

โปรแแกรมเมนู คำนวณตัวเลข 2 ชุด
------------------------------------------------------------------
//menu01
#include"stdio.h"
#include"conio.h"
#include"ctype.h"
main()
{
float a,b,c,d;
int x;
bool T=true;
do{
printf("\n Calculate Number....\n");
printf("---------------------\n");
printf("  1. Plus...  \n");
printf("  2. Minus...  \n");
printf("  3. Multiply...  \n");
printf("  4. Divide...  \n");
printf("---------------------\n");
printf("  5. eXit...  :");
scanf("%d",&x);
switch(x)
{
case 1 : printf("\n Input number1:");scanf("%f",&a);
printf(" Input number2:");scanf("%f",&b);
printf(" %.2f + %.2f = %.2f",a,b,a+b);
break;
case 2 : printf("\n Input number1:");scanf("%f",&a);
printf(" Input number2:");scanf("%f",&b);
printf(" %.2f - %.2f = %.2f",a,b,a-b);
break;
case 3 : printf("\n Input number1:");scanf("%f",&a);
printf(" Input number2:");scanf("%f",&b);
printf(" %.2f x %.2f = %.2f",a,b,a*b);
break;
case 4 : printf("\n Input number1:");scanf("%f",&a);
printf(" Input number2:");scanf("%f",&b);
printf(" %.2f / %.2f = %.2f",a,b,a/b);
break;
case 5 : T=false;
}
}while(T);

printf("Bye bye....")

getch();
}

ตัวอย่างโปรแกรม loop while

โปรแกรมสุ่มตัวเลข
-----------------------------------------------------------------
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "time.h"
#include "math.h"
#include "windows.h"
#define textcolor(txt,back) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), back*16+txt)
#define resetcolor() SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15)
main()
{
srand(time(NULL));
bool  T=true;
int   x;
int   count=1;
int   randomNumber1 = rand() % 100;
while(T)
{
textcolor(9,15);
printf("\n\n<count=%d>Guess the number 0-100 :",count);
resetcolor();
scanf("%d",&x);
if(x==randomNumber1)
{
textcolor(15,12);
printf("\n Bingo... ");
resetcolor();
T = false;
}
else if(x < randomNumber1)
printf("\n Less than... ");
   else
    printf("\n More Than... ");
count++;
}

getch();
}

วิชาออกแบบและเทคโนโลยี 1, 2 ปี 2567

0.1 ภาคเรียนที่ 2/2567 E-mail Address  E-mail Address m.4/3  E-mail Address m.4/2  E-mail Address m.4/1  สมัครเข้ากลุ่ม CANVA PTD GROUP Emai...