BC 322 5 while do while BC 322
BC 322 ���� 5 ������ while ������ do. . while BC 322 : Computer Programming (ครงท 5) 1
Ex 1 ����������� 1 – 10 ������� #include<stdio. h> void main() 1 { int i = 1; while (i <= 10) { printf(“i = %d”, i); i=i+1; 3 } getch(); } BC 322 : Computer Programming (ครงท 2 5) 7
Ex 2 ������������������� 20 �� ���������� Input : . . Output : . . . . START ตวอยางผลรน Enter weight : 45. 7 Enter weight : 67. 2. . Sum of weight = 652. 7 kg. BC 322 : Computer Programming (ครงท 5) 8
Solution BC 322 : Computer Programming (ครงท 5) 9
Ex 3 ������� 1 -10 #include <stdio. h> #include <conio. h> void main() { int num, sum; 1 clrscr(); num = 1; sum = 0; while (num <= 10) { sum =sum+ num; num=num+1; } printf( "%dn", sum ); BC 322 } : Computer Programming (ครงท 2 ���. . . 3 5) 10
*/Find the average of scores */ #include <stdio. h> void main() { Ex 4 โปรแกรมคำนวณคาเฉล ยของคะแนน 3 คา int count; จงเตมคำสงทขาดห float score, total. . . , average; ายไป } . . . . while (. . . ) { printf( "Enter score: " ); scanf(. . . . . ); total =total+ score; . . } average =. . . . . printf( "The average is %6. 2 fn", average ); 11 BC 322 : Computer Programming (ครงท 5)
Ex 5 ����������� 0 �������� #include<stdio. h> void main() { int age, Sum=0; printf(“Enter Age (0 to stop) : “); scanf(“%d”, &age); 1 while (age != 0) 2 { Sum=Sum+age; printf(“Enter Age (0 to stop) : “); scanf(“%d”, &age); 3 } printf(“Summary is %d”, Sum); BC 322 : Computer Programming (ครงท 5) 13
START Flowchart Ex 5 BC 322 : Computer Programming (ครงท 5) 14
����������������������� ������������ 0 ������������� Enter amount : 2 Enter price : 20 You must pay 40 baht ********* Enter amount : 8 Enter price : 60 You must pay 480 baht ********* Enter amount : 0 ********* Thank you BC 322 : Computer Programming (ครงท Input. . . : . . . Output. . . : . . . . 5) 15
Solution Ex 6 BC 322 : Computer Programming (ครงท 5) 16
flowchart do. . while ������ Y ���� N ������ BC 322 : Computer Programming (ครงท 5) 19
Ex 7 ����������� 1 – 10 ������� #include<stdio. h> void main() { int i = 1; do { printf(“i = %d”, i); i=i+1; } while (i <= 10); } BC 322 : Computer Programming (ครงท 5) 20
Solution Ex 8 BC 322 : Computer Programming (ครงท 5) 22
������ for, while Ex 9 โปรแกรมแสดงคำวา #include<stdio. h> void main() { for(i=1; i<=5; i++) { printf(“Hellon”); printf(“*******n”) ; } } BC 322 : Computer Programming (ครงท Hello 5 ครง #include<stdio. h> void main() { i=1; while(i<=5) { printf(“Hellon”); printf(“*******n”); i=i+1; } } 5) 25
Ex 10 ����������� � 10 �� ������� �. �. ���������������������� Enter Year 1 : 2530 Student 1 is 20 year’s old. ********** Enter Year 2 : 2532 Student 1 is 18 year’s old. **********. . เขยนโปรแกรม 2 ขอ 1. ใช for 2. ใช while Enter Year 10 : 2528 Student 1 is 22 year’s old. ********** BC 322 : Computer Programming (ครงท 5) 26
Solution Ex 10 แบบ for BC 322 : Computer Programming (ครงท 5) 27
Solution Ex 10 แบบ while BC 322 : Computer Programming (ครงท 5) 28
���� Test ������ while (����� ) BC 322 : Computer Programming (ครงท 5) 29
- Slides: 29