รบกวนเทพภาษาซีเชิญทางนี้หน่อยคับ

เริ่มโดย rockyrich, 25 มกราคม 2012, 19:42:00

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

rockyrich


คำสั่ง while หรือ do while ในภาษาซีคับ

ให้โปรแกรม  รับค่าจำนวนเต็ม มีค่ามากกว่า 0 มา 2 ค่า คือ ค่า a และ b
คือ a ต้อง มากกว่า b หาก b มากกว่า ให้กลับให้ป้อนค่าใหม่
เมื่อป้อนให้ a มากกว่า b

เช่น a =10 b=20 ให้โปรแกรม นับเลข 10 -20 ผลลัพท์คือ
10 11 12 13 14 15 16 17 18 19 20

รบกวนดูให้ผมหน่อย ปวดหัวคับ ทั้งๆที่มันไม่ยากเท่าไหร่แต่คิดไม่ออกจริงๆ

#include<stdio.h>
#include<conio.h>
void main()
{
int start,stop;
int plus=0;
int p=1;

do{
clrscr();
printf("\nStart Number=");scanf("%d",&start);
printf("\nStart Number=");scanf("%d",&stop);
   p=p+plus;
   start=start+p;
   plus+1;

}while(start>=stop);
printf("\nAnswer= %d",start);
getch();
}

shellshock

ดันช่วยครับ ขอให้มีคนมาตอบเร็วๆครับ

Jupitor

คร่าวๆนะครับ
เขียนภาษาอื่นอยู่
แต่เคยเรียนนานแล้ว

#include<stdio.h>
#include<conio.h>
void main()
{
int start,stop;
int p=1;
clrscr();
printf("\nStart Number=");scanf("%d",&start);
printf("\nStart Number=");scanf("%d",&stop);

do{
   printf("\nAnswer= %d",start);
   start=start+1;
 
}while(start>stop);
getch();
}
[direct=http://www.sevenpassenger.com]Seven Passenger Car[/direct]

rockyrich


rockyrich

อ้างถึงจาก: Jupitor ใน 25 มกราคม 2012, 19:56:27
คร่าวๆนะครับ
เขียนภาษาอื่นอยู่
แต่เคยเรียนนานแล้ว

#include<stdio.h>
#include<conio.h>
void main()
{
int start,stop;
int p=1;
clrscr();
printf("\nStart Number=");scanf("%d",&start);
printf("\nStart Number=");scanf("%d",&stop);

do{
   printf("\nAnswer= %d",start);
   start=start+1;
 
}while(start>stop);
getch();
}

ขอบคุณครับ สำหรับคำตอบ แต่รู้สึกว่ามันยังไม่ออกนะคับ รันกับ turbo c++ คับ

gobank01

#5
#include<stdio.h>
#include<conio.h>
void main()
{
int stop,start;
int plus=0;
int p=1;

While(1){
   scanf("%d",&start);
   scanf("%d",&stop);
   if(stop>start){
      break;
   }
}

while(start<=stop){
printf("\nAnswer= %d",start);

start++;

getch();
}

:-[ ถ้าผิดอย่าว่า เค้านะ

nattapol196

#6
อ้างถึง#include<stdio.h>
#include<conio.h>

void main()
{
int start,stop;
do{
printf("\nStart Number=");scanf("%d",&start);
printf("\nStop Number=");scanf("%d",&stop);
}while(start>=stop);

printf("\nAnswer= ");
while(start<=stop){
   printf("%d ",start);
   start=start+1;
}
getch();
}

rockyrich

อ้างถึงจาก: gobank01 ใน 25 มกราคม 2012, 20:08:46
#include<stdio.h>
#include<conio.h>
void main()
{
int stop,start;
int plus=0;
int p=1;

While(1){
   scanf("%d",&start);
   scanf("%d",&stop);
   if(stop>start){
      break;
   }
}

while(start<=stop){
printf("\nAnswer= %d",start);

start++;

getch();
}

:-[ ถ้าผิดอย่าว่า เค้านะ

ขอบคุณมากมายคับ แต่ยังไม่ออก ขอบคุณคับ ด้านล่างมันออกนะคับ แต่ อยากให้มัน
ป้อนค่าใหม่ เมื่อค่าแรกที่ป้อนมากกว่าค่าที่สอง เช่น ป้อน start=10 stop=5
ให้กลับไปป้อนใหม่จนกว่า  start น้อยกว่า stop แล้วแสดงผลออกมา
เช่น start=5 stop=10 ผลออกมาคือ 5 6 7 8 9 10 ประมาณนี้คับ  


#include<stdio.h>
int main()
{
  int a, b, i;
  printf("Enter to start : ");
  scanf("%d", &a);
  printf("Enter to stop : ");
  scanf("%d", &b);

  while(a <= b)
  {
      printf("%d ", a);
      a++;
  }
  getch();
  return 0;
}

rockyrich

อ้างถึงจาก: nattapol196 ใน 25 มกราคม 2012, 20:21:59
อ้างถึง#include<stdio.h>
#include<conio.h>

void main()
{
int start,stop;
do{
printf("\nStart Number=");scanf("%d",&start);
printf("\nStop Number=");scanf("%d",&stop);
}while(start>=stop);

printf("\nAnswer= ");
while(start<=stop){
   printf("%d ",start);
   start=start+1;
}
getch();
}

nattapol196 ถูกต้องคับตรงเปะเลย ขอบคุณมากมายคับ ขอบคุณ คุณ gobank01 คุณ Jupitor
คุณ shellshock พี่น้องชาวไทยเสียวน้ำใจไม่เคยเหือดแห้งจริงๆ

sleepcat


#include<stdio.h>
#include<conio.h>
void main()
{
   int start,stop,i;
   printf("Enter start:");
   scanf("%d",&start);
   do{
     printf("Enter stop:");
     scanf("%d",&stop);
   }while(start<=stop);
   i=start;
   while(i<=stop){
     printf("%d ",i);
     i++;
   }
getch();
}

ลองดูนะครับ