Top Banner
1 2 2 () 4 10 --------------------------------------------------------------------------------------------------------- 1. \n . 1 . 1 . . 2. . %c . %d . %e . %f 3. int m=5, n=8; printf(%d %d\n, n,m); . 0 1 . 5 8 . 5 3 . 8 5 4. . float num value =2.2; printf(%f,num value); . int extra = 30000; printf(e = %d,extra); . float min = -0.5832; printf(%.2f\n,min); . int few = -30000; printf(f = %d,few); 5. int c,k; double x,y,z; x=1.2; y=2.1; z= 3.2; c=3; z=x+y+z; k=z/c; printf(%f %f %f %d %f,x,y,z,k,z/c);
46

แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

Sep 12, 2014

Download

Education

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

1 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� ���� ���������� 2 (������������������� ����) � !� " �#$�%��&��' 4�)'�*

�+�� '*� �������*,�-.��/� ��� 10 ���

--------------------------------------------------------------------------------------------------------- �+���!�2* ��������� ��������������������������������������� 1. ���!"�����#��$���% \n �����������"&

�. ����!"������������� 1 ���

!. ���'(���(��)*�"�" 1 ����

�. !+,�������#(

�. ���"�"'(���(��

2. ����)$��!��#� ����'����!��#� )".� !�/�0�# �������)$��!��#� �� �. %c !. %d

�. %e �. %f

3. !����)*�% ��234���(��!��5)$�#����(�&)��, int m=5, n=8; printf(C%d %d\nG, n,m); �. 0 1 !. 5 8

�. 5 3 �. 8 5 4. !�����(�&)��,%0� �. float num value =2.2; printf(C%fG,num value); !. int extra = 30000; printf(Ce = %dG,extra); �. float min = -0.5832; printf(C%.2f\nG,min); �. int few = -30000; printf(Cf = %dG,few); 5. �����5)$�##����������(�!�����$) ����(�&)��, int c,k; double x,y,z; x=1.2; y=2.1; z= 3.2; c=3; z=x+y+z; k=z/c; printf(C%f %f %f %d %fG,x,y,z,k,z/c);

Page 2: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

2 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

% ��23���$������#��������4������!���� �. 1.20000 2.100000 3.200000 2 1.000000 !. 1.20000 2.100000 6.500000 2 2.000000 �. 1.20000 2.100000 6.500000 2 2.166667 �. 1.20000 2.100000 3.200000 2 1.066667 6. '�0�!�����$)�����������#�)$����$���% !�����$)��!�������&#(���� ������ �. %x ��� char !. %f ��� double �. %.2f ��� float �. &#(#�!����%0� 7. !���� )*�% ��234�������� Run 5)$�#!��� (����,

#include <stdio.h> #include <conio.h> void main() { clrscr(); printf(" TURBO C "); printf(" is easy programming. "); getch(); } �. TURBO C !. TURBO C is easy programming. �. is easy programming. �. TURBO C is easy programming

8. 4���(��!��5)$�#�(�&)��, float a,b; printf(Cinput a=G); scanf(_____________!����� 1_____________) printf(Cinput b=G); scanf(_____________!����� 2_____________) printf(Ca+b=%fG,a+b); �0�l���!�����������0#!�����#�� �&) �. !�� 1 �0# C%fG,&a $ "!�� 2 �0# C%fG,&b !. !�� 1 �0# C%fG,a $ "!�� 2 �0# C%fG,&b �. !�� 1 �0# C%fG,*a $ "!�� 2 �0# C%fG,*b �. !�� 1 �0# C%fG,a $ "!�� 2 �0# C%fG,b

Page 3: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

3 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

9. ������$) num )*����$)'�0� int $ " temp )*����$)'�0� double �����,� ���4"���������� �(�&)��,������&����(��������� �����0#!�����#���(�&)��, scanf(C%lf %dG,___________); �. num,temp !. &temp, &num �. temp, num �. &num,&temp 10. !����)*�% ��234���(��!��5)$�#����(�&)��, printf(C%9sG,GHELLOG);

�. H E L L O

!. H E L L O

�. H E L L O

�. H E L L O

Page 4: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

4 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�+�� '*� �������*,�-.��/�

������*,� (Output) ��$���% ��23 )��0�s���$���������4�.�� ��.�t�u��'����)!��vw��3'�� '(��������������� x 5��vw��3'��#��y�����#��� library vw��3'������'�$���% ���4�.�� '(� printf() ,puts() , putchar() vw��3'�� � (���,���(�� header file '��� stdio.h 78*��� printf() )*�vw��3'������'�$���% ��23������4�.�� ��$���% ���4�.������vw��3'����,��#���������)$��$ "��������#&�� 5��vw��3'�� printf() #��)$�� �����,

printf ("format",variable);

format

!��#� ���������$���% ����������4� 5��!��#� ��,����!���&����������#�� " " !��#� �����#��$���% &��#����( 2 )".� ��� !�����#2#�� $ "�(�����s�&�������$) u+�����)*��(�����s�&�������$)������(��������#�)$����������'�0�!��!��#� ����s�&�������$)��,�����

variable ���$)����0�4�3�������������(�&)$���% ����������������#�)$�����������&��

����)$������'����vw��3'�� printf() 4"0�#����������#�� % �������$����(�&)��,

����)$�� ���#�#������������!������)$��

%c �'����������$��(����4"$��� ���!" 1 ��� (single character)

%d �'����������$��(�$��� !4������s#(integer��� int ) 1 4��������) !y���0�

%e �'�$������ !���)*�4���/�0�# (floating point ,float)���) e '(� 2.15e+2 ���$���(�215

%f , %lf �'����!��#� )*� float $ " double

%g �'����!��#� float

%h �'���� short integer

%o(���5�) �'�$��� integer ���) !y��$)�

%x �'�$��� integer ���) !y���0���

%s �'�$���!�����#

%p �'�$��� address !�����$)����3��3

Page 5: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

5 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

��������#����'���� printf() (�����������������#�� \ )

��������# ���#�#��

\a �(�������z�

\b �������t������(����u���1���$��(� (backspace)

\t �������$��(������0#�3��,��(�&) 1 $�s�($���� horizontal)

\n !+,�������#(

\v �������$��(�&) 1 $�s�$���0��

\r ����&)�������

\" $���������#�� "

\' $���������#�� '

\? $���������#�� ?

\\ $���������#�� \

�����(�����'�������� printf �����,

printf("Hello Program C"); $���!�����# Hello Program C ������!�.��

printf("ChalermKwan Stree school"); $���!�����# ChalermKwan Stree school ������4�.�� printf("Phitsanuloke Thailand"); $���!�����# Phitsanuloke Thailand ������4�.�� printf(C1+1 = %dG,2); $���!�����# 1+1 = 2 ������4�.�� char x =�x�; printf(CHello %cyzG,x

$���!�����# Hello xyz ������4�.��

int x = 3; char y[5] = CfiveG; printf(C%d + %d = %sG,2,x,y

2 + 3 = five �20��� 1. 4����('�0�!��#� %d !������(������ (�(� 2) 2. 4����('�0�!��#� %d !��������$) x u+��)*�!��#� '�0� 4������s# 3. 4����('�0�!��#� %s !������(������ y u+��!��#� '�0���0�

printf(C%d\nG,233); printf(C%05d\nG,233); printf(C%f\nG,233.33); printf(C%.2f\nG,233.33);

$���% ���#�)*� 233 $���% ���#�)*� 00233 $���% ���#�)*� 233.330000 $���% ���#�)*� 233.33

Page 6: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

6 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�����(�����'�������� printf (�(�)

printf(C%8dG,245); $������ !4������s# 245 ����������4� 5��� ����,�������u��� &�� 5 '(��

printf(C%9sG,GGrassG); $���!�����# Grass ����������4� 5��� ����,����u���&�� 4 '(��

printf(C%10cG,�z�); $���������t z ����������4� 5��� ����,�������u���&�� 9 '(��

printf(C%12fG,11.235); $������ !�/�0�# 11.235000 ����������4� 5��� ����,�������u���&�� 6 '(��

printf(C%-8dG,245); $������ !4������s# 245 ����������4� 5��� ����,�������!�� &�� 5 '(��

printf(C%-9sG,GGrassG); $���!�����# Grass ����������4� 5��� ����,����!��&�� 4 '(��

printf(C%-10cG,�z�); $���������t z ����������4� 5��� ����,�������!��&�� 9 '(��

printf(C%-12fG,11.235); $������ !�/�0�# 11.235000 ����������4� 5��� ����,�������!��&�� 6 '(��

�+�� '* putchar() ����$���% ������t������!" ( char ) ����������4� ���4���'�������� printf ���#�����������������#�)$�� %c $ �� ���#������'��������������$���������t������!"5�����"&��������� 5�����������,���� ������� putchar() u+��#��)$��������'�������� ���$����(�&)��,

putchar(ch); ch : ������t������!"!������(.����������#�� �c' ������$)'�0� char

Page 7: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

7 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�����(�����'�������� putchar() ����$���% ���!"����������4� $���&�������,

�+�� '* puts() )*��������������$���% !�����#����������4� #��)$�����'�������������,

puts(str); str : !�����#u+��!������(.����������#�� C G ������$)����s�!��#� '�0�!�����#

�����(�����'����������� puts() ����$���!�����#����������4� $���&�������,

� �-.��/�2���� ������ ��������!��5)$�#�(�����(#��4")*���'���#5�����%���'�$�� 2 �0/��� �����s��� ��,�.��!��$���% ������������������4� $ ".��!������!��#� 4��%���'�!��#�������3��3� ����(�#����)"#� % !��5)$�# u+����.��!������!��#� 4��%���'� .�t�u��������������$ "vw��3'��#��y����&���������'�$ �� '(��������.��!����$���% �� "����!���������� (���,&��$�(

�+�� '* scanf() ��.�t�u� ����!��#� 4�����3��3���#�����&��5��������'�vw��3'�� scanf() u+��)*�vw��3'��#��y����������!��#� 4�����3��3� 5����#����!��#� &�����)".� &#(�(�4")*�4������s# ( int ) , �/�0�# ( float ) ,���!" ( char ) ���!�����#�s��# �)$��������'�������� scanf() � ������������'�������� printf() ���$����(�&)��,

Page 8: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

8 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

scanf(CformatG , &variable) ; format : ���'���������#�)$�� ����������'�0�!��!��#� ���4"��!��#�4�����3��3� 5����������#�)$��4"�'�'�����������������#�)$��!��������� printf() variable : ���$)���4"�'��s��(�!��#� �����!��#�4�����3��3� 5��'�0�!�����$)4"�����������������#�)$�����������&�� ���4����, ����'���!�����$)4"���������������������#�� & ��������$)��0��������s�!�����#�(���,����&#(���������������������#�� &

�����(�����'����������� scanf() ������!��#� 4�����3��3� $���&�������,

�+�� '* getchar() ��� getch() ��,� 2 ���������,�'���!��#� )".����!"( char ) 5��#��)$��������'�������������,

ch = getchar(); ch = getch();

ch : ���$)'�0� char �����'��s��(�!�����!"���������t�����!��#�

Page 9: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

9 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�+�$#��(���,�������� getchar() $ " getch() 4"�'���������!��#� '�0����!"�#������ $�(��,� 2 ���������,#����#$���(��������(����� ������� getchar() #���)������!"!��#�$ �� ������)��# <Enter> 5)$�#4+�4"� ��&)�������(�&�� $ "������!"����)���4"$���!+,�#�����s�������4����� �(��������� getch() &#(������)��# <Enter> ����$�(�)������!"!��#� 1 ��� 5)$�#4"� ��&)�������(������ $ "���

���!"����)���4"&#($���!+,�#�����s�

�����(��������'�������� getchar() ��������t!��#�4�����3��3� $���&�������,

% ��������!��5)$�#�����&���(� �������)��# <Enter> � ��4�����)���������t$ �� ���4����,������t���)���4"$���!+,�#�������4�����

4��5)$�#�0# #���) ����4��������� getchar() )*� getch() % ��234����������$���&�������,

Page 10: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

10 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

4"�����&���(� #����'�������� getch() �&#(������)��# <Enter> ��#� ����)���������t $ "4"&#(�s�������t����)���$���!+,�#�������4�

�+�� '* gets() ���4���������������!��#� '�0����!"���������t$ �� ����!�����#4�����3��3��s#���������������'�'(���� ��� ������� gets() u+��#��)$��������'�����(�&)��,

gets(str); str : ���$)�������s�!�����#�����!��#�4�����3��3� u+��4"�����������#&���(�����4"����'�������� gets()

Page 11: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

11 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�����(��������'�������� gets() ��!�����##�4�����3��3� $���&�������,

Page 12: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

12 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� M.�2�1 (print1) ���������� � ���

�2� � ������ ����������������������� �� Hello C Language By Student

-.��/�+�-.�

-

-.��/���*���

����������./�������������� “Hello C Language By Student” (����������������0�1�)

� �� ��*��' 1 -.��/���*���

-.��/�+�-.�- Hello C Language By Student

Page 13: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

13 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L� � �%����������� M.�2�2 (print2) ���������� � ���

�2� � ������ ����������������������� �� Welcome to Programming Hello C Language

-.��/�+�-.�

-

-.��/���*��� 8������������ ���./���� “Welcome to Programming” (����������������0�1�) ���./�.����� “Hello C Language” (����������������0�1�)

� �� ��*��' 1 -.��/���*���

-.��/�+�-.�- Welcome to Programming Hello C Language

Page 14: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

14 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� M.�2�3 (print3) ���������� � ���

�2� � ������ ����������������������/��� num1 ���.�� ��� �0� �8���/��� num1 ��: �;��0 ��<� �����.��/� 50

-.��/�+�-.�

-

-.��/���*��� ����������/��� num1

� �� ��*��' 1 -.��/���*���

-.��/�- 50

Page 15: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

15 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� M.�2�4 (print4) ���������� � ���

�2� � ������ ����������������������/��� word1 ���.�� ��� �0� �8�� word1 ��: �/����/���@������: B

-.��/�+�-.�

-

-.��/���*��� ����������/��� word

� �� ��*��' 1 -.��/���*���

-.��/�+�-.�- B

Page 16: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

16 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� M.�2�5 (print5) ���������� � ���

�2� � ������ ����������������������/��� num1 ���.�� ��� ���8�������/�;��;/��A�.B C����� 2 �0�� �� �0� �8���/��� num1 ��: �0 .B C�� �����.��/� 5.646555

-.��/�+�-.�

-

-.��/���*��� ����������/��� num1

� �� ��*��' 1 -.��/���*���

-.��/�+�-.�- 5.65

Page 17: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

17 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� M.�2�6 (print6) ���������� � ���

�2� � ������ �������������/��1�������/���@8����H;��@.0���1�

-.��/�+�-.�

-

-.��/���*���

�0� ��/��� char data[] = “basic operation”

� �� ��*��' 1 -.��/���*���

b a s i c o p e r a t i o n b a s i c b a s i c o p e r a t i o n

Page 18: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

18 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� �� ������ 1(scanf1) �/�����1;�����I���I�

�2� � ������ ��������/��/�;����� 3 �0 �������<���8 �/��� num1 num2 �;@ num3 ��������K� ����.�;@�0 �;@�����/�;�./�����0

-.��/�+�-.� �� 3 ���./� ���./�.�� 1 �/��� num1 ��: �/�;��0 ��<� ���./�.�� 2 �/��� num2 ��: �/�;��0 ��<� ���./�.�� 2 �/��� num3 ��: �/�;��0 ��<�

-.��/���*��� ��������1;�/�;�./�����0

� �� ��*��' 1 -.��/���*���

3

4 5

3,4,5

122 345 698

122,345,698

Page 19: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

19 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� �� ������ 2(scanf2) �/�����1;�����I���I�

�2� � ������ �������������/�����1; �0� /��;@�� �1� �����./����������1; �0� /��;@�� �1�

-.��/�+�-.� �� 2 ���./�

���./�.�� 1 �/��� weight ��: �/�;��0 ��<� ���./�.�� 2 �/��� high ��: �/�;��0 ��<�

-.��/���*��� ��������1;��� �0� /��;@�� �1�

� �� ��*��' 1 -.��/���*���

Weight : 45

High : 162

Weight & High is 45,162

Weight : 70

High : 165

Weight & High is 70,165

Page 20: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

20 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� �� ������ 3(scanf3) ���������� � ���

�2� � ������ �������������/�����1; ��/��C �� T����C �� ������ �� �;@�0 .��U��� �� /� 8������H;���.���V� ������1;.������������ ��/��C �� T����C �� ������ �� �0 .��U��� �;@����./����� �������./����� = ������ �� * �0 .��U���

-.��/�+�-.� �� 4 ���./� ���./�.�� 1 �/��� Id ��: �/�/���@ ��������� 2 ���� Name �� �������������

��������� 3 ���� Unit Price �� ���������������� ������� 4 ���(� Quatity )*���� !4�����40�

-.��/���*��� ��������1;��/��C �� T����C �� ������ �� �0 .��U��� �;@����./�����

� �� ��* 1111 -.��/�+�-.� -.��/���*���

Enter Product Id = B05 Enter Product Name = NoteBook Enter Product Unit Price = 45 Enter Quatity = 3

Id = B05 Name = NoteBook Unit Price = 45 Quatity = 3 Total Amount = 135.00

Page 21: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

21 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� �� ������ 4(scanf4) ���������� � ���

�2� � ������ ��������0 Z���C������.���;� �0� �8�� pi = 3.14 �1���0 Z��C������.���;� = 4/3*pi*radius*radius*radius (�0� �8�� 4/3 ������@�Z = 1.33)

-.��/�+�-.� �� 1 ���./� ���./�.�� 1 �/����/B�����.���;�

-.��/���*��� �����./��������������C������.���;�

� �� ��* 1111 -.��/�+�-.� -.��/���*���

5 522.025024

� �� ��* 2222 -.��/�+�-.� -.��/���*���

0.2 0.033410

Page 22: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

22 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� M����Q��' (average1) �0 Z����`;���

�2� � ������ ��������������<�����1;����@� �����<��@� CT���� ������� ./����� 5 ��/�� �����./���0 Z����`;���

-.��/�+�-.� �� 2 ���./� ���./�.�� 1 �/����@� �����/��.�� 1 ��: �;��0 ��<� ���./�.�� 2 �/����@� �����/��.�� 2 ��: �;��0 ��<� ���./�.�� 3 �/����@� �����/��.�� 3 ��: �;��0 ��<� ���./�.�� 4 �/����@� �����/��.�� 4 ��: �;��0 ��<� ���./�.�� 5 �/����@� �����/��.�� 5 ��: �;��0 ��<�

-.��/���*��� �������`;�������@� ��<�

� �� ��* 1 -.��/�+�-.� -.��/���*���

12 13

14 18 20

average = 15

� �� ��* 2 -.��/�+�-.� -.��/���*���

23 24

28 29 21

average = 25

Page 23: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

23 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� �)!��'��'M��' � (area1)(area1)(area1)(area1)) �0 Z���� .������1������;����

�2� � ������ ��������0 Z���� .���1������;���� �������H;���0 Z����8 �1���� .B C�� 2 �0�� �� �0� ��1���0 Z��� .���1������;���� = ��� X �

-.��/�+�-.� �� 2 ���./� ���./�.�� 1 �/����� ��� ���./�.�� 2 �/����� �

-.��/���*��� �����./��������������� .������1������;����

� �� ��* 1 -.��/�+�-.� -.��/���*���

2 4

6

� �� ��* 1 -.��/�+�-.� -.��/���*���

4 10

40

Page 24: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

24 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� �)!��'���M��' � 2(area2)(area2)(area2)(area2) �0 Z���� .������1�����;����

�2� � ������ ��������0 Z���� .���1�����;����

�0� ��1���0 Z��� .���1�����;���� = 0.5 xc X �1�

-.��/�+�-.� �� 2 ���./� ���./�.�� 1 �/���c

���./�.�� 2 �/������1�

-.��/���*��� �����./��������������� .������1�����;����

� �� ��* 1 -.��/�+�-.� -.��/���*���

0.25

5

1.250000

� �� ��* 1 -.��/�+�-.� -.��/���*���

15 5

37.500000

Page 25: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

25 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� �.����*-�*�*��� 3(area3)3(area3)3(area3)3(area3) �0 Z���� ���������;�

�2� � ������ ��������0 Z���� ���������;� �������H;���0 Z��: .B C�� 2 �0�� ��

�+�M�RM.

�1���0 Z��� ���� circumference = 2¶r ¶ = 3.14

-.��/�+�-.� �� 1 ���./� ���./�.�� 1 �/����/B�������;�

-.��/���*��� �����./������������H;���0 Z��� ����

� �� ��* 1 -.��/�+�-.� -.��/���*���

12 75.36

� �� ��* 1 -.��/�+�-.� -.��/���*���

.5 3.14

Page 26: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

26 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� ����0����(price) �����l������0�����#��,��#�

�2� � ������ �������������/�����1;�/� �� ��������./�����

-.��/�+�-.� �� 2 ���./� ���./�.�� 1 �/���/��C �� ������� 2 ��'����0���� ������� 3 ������(���(�� ������� 4 4��������u�,�

-.��/���*���

�����./� 5 ����

� �� ��* 1 -.��/�+�-.� -.��/���*���

Input product id : b05

Input product name : notebook

Input product unit price : 45

Input quantity : 3

Id : b05

Name : notebook

Unit price : 45

Quantity : 3

Total amount : 135.00

Page 27: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

27 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

��������M� *�� ���� ���������� 2 (������������������� ����) � !� " �#$�%��&��' 4�)'�*

�+�� '*� �������*,�-.��/� ��� 10 ���

--------------------------------------------------------------------------------------------------------- �+���!�2* ��������� ��������������������������������������� 1. ���!"�����#��$���% \n �����������"&

�. ����!"������������� 1 ���

!. ���'(���(��)*�"�" 1 ����

�. !+,�������#(

�. ���"�"'(���(��

2. ����)$��!��#� ����'����!��#� )".� !�/�0�# �������)$��!��#� �� !. %c !. %d

�. %e �. %f

3. !����)*�% ��234���(��!��5)$�#����(�&)��, int m=5, n=8; printf(C%d %d\nG, n,m); �. 0 1 !. 5 8

�. 5 3 �. 8 5 4. !�����(�&)��,%0� �. float num value =2.2; printf(C%fG,num value); !. int extra = 30000; printf(Ce = %dG,extra); �. float min = -0.5832; printf(C%.2f\nG,min); �. int few = -30000; printf(Cf = %dG,few); 5. �����5)$�##����������(�!�����$) ����(�&)��, int c,k; double x,y,z; x=1.2; y=2.1; z= 3.2; c=3; z=x+y+z; k=z/c; printf(C%f %f %f %d %fG,x,y,z,k,z/c);

Page 28: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

28 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

% ��23���$������#��������4������!���� �. 1.20000 2.100000 3.200000 2 1.000000 !. 1.20000 2.100000 6.500000 2 2.000000 �. 1.20000 2.100000 6.500000 2 2.166667 �. 1.20000 2.100000 3.200000 2 1.066667 6. '�0�!�����$)�����������#�)$����$���% !�����$)��!�������&#(���� ������ �. %x ��� char !. %f ��� double �. %.2f ��� float �. &#(#�!����%0� 7. !���� )*�% ��234�������� Run 5)$�#!��� (����,

#include <stdio.h> #include <conio.h> void main() { clrscr(); printf(" TURBO C "); printf(" is easy programming. "); getch(); } �. TURBO C !. TURBO C is easy programming. �. is easy programming. �. TURBO C is easy programming

8. 4���(��!��5)$�#�(�&)��, float a,b; printf(Cinput a=G); scanf(_____________!����� 1_____________) printf(Cinput b=G); scanf(_____________!����� 2_____________) printf(Ca+b=%fG,a+b); �0�l���!�����������0#!�����#�� �&) �. !�� 1 �0# C%fG,&a $ "!�� 2 �0# C%fG,&b !. !�� 1 �0# C%fG,a $ "!�� 2 �0# C%fG,&b �. !�� 1 �0# C%fG,*a $ "!�� 2 �0# C%fG,*b �. !�� 1 �0# C%fG,a $ "!�� 2 �0# C%fG,b

Page 29: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

29 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

9. ������$) num )*����$)'�0� int $ " temp )*����$)'�0� double �����,� ���4"���������� �(�&)��,������&����(��������� �����0#!�����#���(�&)��, scanf(C%lf %dG,___________); �. num,temp !. &temp, &num �. temp, num �. &num,&temp 10. !����)*�% ��234���(��!��5)$�#����(�&)��, printf(C%9sG,GHELLOG);

�. H E L L O

!. H E L L O

�. H E L L O

�. H E L L O

Page 30: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

30 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

S��,��

Page 31: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

31 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

Q� ������������� ��� 1 �)'�*���������$ "$���% !��#�

�� ���� * 30250 ���������� 2(���������������� ����) ��� �� !� " �#$�%��&��' 4

-.� ���

1. � 2. � 3. � 4. � 5. � 6. � 7. ! 8. � 9. !

10. �

Page 32: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

32 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

������������� �������������� ������ ��������������� � !��"�

���� L�� �%����������� M.�2�1 (print1) # include <stdio.h>

# include <conio.h>

int main ()

{

printf("Hello C Languade By Student") ;

getch() ;

}

���� L�� �%����������� M.�2�2 (print2) # include <stdio.h>

# include <conio.h>

int main ()

{

printf("Welcome to Program\n") ;

printf("Hello C Languade");

getch() ;

}

���� L�� �%����������� M.�2�3 (print3) # include <stdio.h>

# include <conio.h>

int main ()

{

int num1 ;

num1 =50;

printf("%d",num1);

getch() ;

}

���� L�� �%����������� M.�2�4 (print4)

# include <stdio.h>

# include <conio.h>

int main ()

{

char word1;

word1='B';

printf("%C",word1);

getch ();

}

Page 33: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

33 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

���� L�� �%����������� M.�2�5 (print5) # include <stdio.h>

# include <conio.h>

int main ()

{

float num1 ;

num1= 5.646555 ;

printf ("%.2f",num1) ;

getch () ;

}

���� L�� �%����������� M.�2�6 (print6) #include <conio.h>

#include <stdio.h>

int main()

{

char data[]="basic opertion";

printf("%s\n",data);

printf("%.5s\n",data);

printf("%23s",data);

getch();

}

���� L�� �%����������� �� ������ 1(scanf1)

# include <stdio.h>

# include <conio.h>

int main ()

{

int num1 ,num2,num3;

scanf(“%d”,&num1);

scanf(“%d”,&num2);

scanf(“%d”,&num3);

printf ("%d , %d , %d ",num1.num2,num3) ;

getch () ;

}

Page 34: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

34 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

���� L�� �%����������� �� ������ 2(scanf2) # include <stdio.h>

# include <conio.h>

int main ()

{

int weight ,high;

printf(“weight “); scanf(“%d”,&weight);

printf(“height “); scanf(“%d”,&high);

printf (“weight & high is %d , %d ", weight ,high) ;

getch () ;

}

���� L�� �%����������� �� ������ 3(scanf3) #include <stdio.h>

main()

{

char ID[10],name[20];

float price;

int quantity;

printf("Enter Product ID : ");

scanf("%s",ID);

printf("Enter Product Name : ");

scanf("%s",name);

printf("Enter Product Unit Price : ");

scanf("%f",&price);

printf("Enter Quantity : ");

scanf("%d",&quantity);

system("cls");

printf("ID:%s”,ID);

printf(Name:%s”,name);

printf( Unit Price:%.2f”,price);

printf( Quantity:%d”,quatity);

printf( Total Amount:%.2f",price*quantity);

getch();

}

Page 35: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

35 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

���� L�� �%����������� �� ������ 4(scanf4)

# include <stdio.h>

# include <conio.h>

int main ()

{

float radius ,c,pi=3.14;

scanf("%f",&radius) ;

c=1.33*pi*radius*radius*radius;

printf("%f",c) ;

getch () ;

}

���� L�� �%����������� M����Q��' (average1) # include <stdio.h>

# include <conio.h>

int main ()

{

int f,s,t,p,a,c;

scanf("%d",&f) ;

scanf("%d",&s) ;

scanf("%d",&t) ;

scanf("%d",&p) ;

scanf("%d",&a) ;

c=(f+s+t+p+a)/5;

printf("%d",c) ;

getch () ;

}

���� L�� �%����������� �)!��'��'M��' � (area1)(area1)(area1)(area1))

# include <stdio.h>

# include <conio.h>

int main ()

{

int weight,high ;

scanf("%d",&weight) ;

scanf("%d",&high) ;

printf("weight & high is %d",weight,high) ;

getch () ;

}

Page 36: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

36 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

���� L�� �%����������� �)!��'���M��' � 2(area2)(area2)(area2)(area2) # include <stdio.h>

# include <conio.h>

int main ()

{

float b,h,t;

scanf("%f",&b) ;

scanf("%f",&h);

t=0.5*b*h;

printf("%f",t) ;

getch () ;

}

���� L�� �%����������� �.����*-�*�*��� 3(area3)3(area3)3(area3)3(area3)

# include <stdio.h>

# include <conio.h>

int main ()

{

float radius ,c,pi=3.14;

scanf("%f",&radius) ;

c=2*pi*radius;

printf("%.2f",c) ;

getch () ;

}

Page 37: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

37 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

���� L�� �%����������� �������.� (price) #include <conio.h> #include <stdio.h> int main() { char id[10],name[20]; float price; int quantity; float total; printf("Input product id : "); scanf("%s",&id); printf("Input product name :" ); scanf("%s",&name); printf("Input product unit price : "); scanf("%f",&price); printf("Input quantity : "); scanf("%d",&quantity); printf("id : %s\n",id); printf("name : %s\n",name); printf("quantity : %d\n",quantity); printf("price : %.2f\n",price); printf("toatal amount : %.2f",quantity*price); getch(); }

Page 38: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

38 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

Q� ��������M� *�� ��� 2 �)'�*���������$ "$���% !��#�

�� ���� * 30250 ���������� 2(���������������� ����) ��� �� !� " �#$�%��&��' 4

-.� ���

1. � 2. � 3. � 4. � 5. � 6. � 7. ! 8. � 9. !

10. �

Page 39: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

39 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�������������

����..................................................���...................�� ��..............................................

�����.................................................................................................................................

��� ���� �������������������� ����� ���� ������������

�������� �������� ����� ����

��� ����� ���� 10

��� ����������� 10

��������� �������� ����� ����

��������� 15

� � 15

������...........................................

(���� % &��&�) ��()�����*+��

Page 40: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

40 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

���T�����

5���������0���/���3$ "�l0�/���3#� �020 ����.(2547).�������������-� ������� ���������� S�%�U�. �0#�3��,���� 1. �0t���(�����2����0#�3 4�����. 2�����3 )"���% .(2545). ����������S�%�U� �+�M� �*���� �#�����. �0#�3��,���� 1. ������: �#��#�(��0#��5�5 ��(&��-���)���). )".�� '(��&#�.(2549). WX�-� �������2������2� �-.����S�%�U�. �0#�3��,���� 1. ������ : �0t�� &���u� �0�5v �0��0�0���3 u���3 4�����. �(��0�� ��3�0��3.(2550). �/��)��� S�%�U��.� ��*. �0#�3��,���� 1. ������: �0t�� u��s����'��� 4����� (#��'�). ��0� )"���0�0���20�.(2547). �/��)��� S�%�U�. ������: �0t��5)�0'��� 4�����. �)&u�3 http://e-learning.snru.ac.th/els/program1/lesson3/page3_1.html http://e-learning.snru.ac.th/els/program1/lesson3/page3_1.html http://www.thaigoodview.com/library/contest2551/tech02/08/2/webit/p3.html http://www.rayongwit.ac.th/data_math/kind.htm http://thaioop.wordpress.com/ http://www.plaloma3d.com/webboard/viewthread.php?tid=685 http://www.skr.ac.th/~pasakorn/c/lesson/l33.html#va

http://www.programming.in.th/tutorial/index.php http://www.sangsiri.net/wbi/40208_sci/ http://www.eng.su.ac.th/ee/618240/variable.html

http://www.eng.su.ac.th/ee/618240/variable.html

http://www.lks.ac.th/kuanjit/ http://www.rayongwit.ac.th/data_math/kind.htm http://web.en.rmutt.ac.th/computerprogramming/Documents/Presentation/PDF/C.pdf

Page 41: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

41 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

Page 42: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

42 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�+�+� $��������t"��!���5)$�#.�t�u� �30250 �0'���#�0���3 2 (��5)$�#$ "��)"����3) � �(#��"��������������'��$ "��5�5 �� '�,�#�2�#/+�t�)���� 4 (#��� 2 ����

���������$ "$���% !��#� 4�����!+,����������!���5)$�#.�t�u�!�,����!�������� ��,���,���� ���#����t"��,��������������!���5)$�# ����4�����$��������t"��!���5)$�#.�t�u� &�� �����!�,���������4���(��$ ���(�����!+,� ����)*����"���� ���4 ���������#����#���4�(��������$ "#�4���0������(������ �0'���#�0���3 2 (��5)$�#$ "��)"����3) ����)*���(���0���(�$��������t"��!���5)$�#.�t�u� 4")*�)"5�'�3��������%������ �(#��"�������'��$ "��5�5 �� ������'�,�#�2�#/+�t�)���� 4 $ "%��������4&��)*���(����

�����3 #��#�

Page 43: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

43 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

���� e

�)'�* M.�

���$�"������'�$��������t"��!���5)$�#.�t�u��������� � ���$�"������'�$��������t"��!���5)$�#.�t�u������������� ! ��"������ � 4��)"���3������� � $��������(����� 1 ���������$���!��#� 4 ��$���% (output) 4 vw��3'��� printf() 4 ������� putchar() 6 ������� puts() 7 ��!��#� 4�����3��3� 7 ������� scanf() 7 ������� getchar() $ " getch() 8 $��������t"��!���5)$�#.�t�u� 12 $�������� ����� 27 .��%��� 30 $������+�����"$�� 24 �l����# 40

Page 44: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

44 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�+���+����R�.���WX�� �%����-� �������S�%�U��+�M� ���/

$��������t"��!���5)$�#.�t�u� (#��� 2 ���� ���������$ "$���% !��#� (#��, 4������!+,��������������&����������t"��!���5)$�#.�t�u���,����� �0'���#�0���3 2 (��5)$�#$ "��)"����3) � 30250 $ "�����&)�'�����!���5)$�#��"���������!+,� �"��,���������0����t" #����#�� ���#��#�� ��������0�% ����)�0���0��#!�,���� �����, 1. /+�t����'�,$4���������'�$��������t"��!���5)$�#.�t�u����!���4 2. /+�t�$%���4��������� 4��)"���3������� ��"������� �"���������� �����% )"#0�% � ��4�$� (�����������!���4 3. ��4�����#$��������t"��!���5)$�#.�t�u� �)'�* ���������$ "$���% !��#� ������������ Download &����� http://Arnon.chs.ac.th $ "�������$%(�u��� 4. �(���������'�,$4������$ "�������!��%�����$ "������!���� �(�#��� 5. ������������$��������(����� ����)"#0����#���0#!�������� 6. $4�$��������t"��!���5)$�#.�t�u� ���������/+�t� $ "$�"����02����'�$��������t"��!���5)$�#.�t�u� ����������4"&��)�0���0&����(��������� 7. ����0���4�����������#�04�#����������������&����$%���4��������� 8. !l")�0���0�04�# ����)*����)+�t� ������$�"������������#�)w��� 9. �����������������&#(��� ����������$�"��� ���#���#�����/+�t�$��������t"��!���5)$�#.�t�u� �0�#�0#��� ��(�� 10. � ��4��������/+�t�$��������t"��!���5)$�#.�t�u��s4$ �� ��$ "������'(�������) 11. ������+��"$��$�( "��&��������)"#0��������$ "���#�������� ���������&#(%(���l�3 ����4��u(�#�0#���������

Page 45: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

45 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�+���+����R�.���WX�� �%����-� �������S�%�U��+�M� � ���

$��������t"��!���5)$�#.�t�u� (#��� 2 ���� ���������$ "$���% !��#� (#��, ����!+,��������������&��/+�t��������� ���/+�t�(�#�������#�'0���������� ������4"&��)"5�'�34��$��������t"��!���5)$�#.�t�u���#��"������$ "4��)"���3�������������)�0���0��#���$�"����(�&)��,

1. ������������(����"������$ "4��)"���3��������(�� �#�� /+�t�$��������t"��!���5)$�#.�t�u� 2. �����������/+�t���,�������� #���#�!������� ���&#(!���4�����������# ��%��������������'�,$�" ����20����0�#�0# ���!��u����#&�� 3. ��������������$�������� $ "��4���������4��.��%��� ���#�!������� �����#��%������������!���4#���0��!+,�

4. ��/+�t�$��������t"��!���5)$�#.�t�u� (#��,4"&#(� �% ���s4 ��������� !�����#u�������3����)�0���0�04�#

Page 46: แบบฝึกทักษะการเขียนโปรแกรมภาษาซีเล่ม2

46 ������������� �������������� ���� 2 �������� ��������� !�"� #��$�

�����+�� e

.�t�u�)*�.�t����&�������#�0�#5������&) $ "#������������ x �#�"�������'��������)*���,�y������!���5)$�#!�,�����(�&) ��)"#� % ��,�������������#&� �3$ "�0���3 �����3 ���)"#� % �(��u� �#��,��)$������!������������,�������!���5)$�#.�t�u� ���#���������$) '�0� � ������,�'��� $ "��)"��/���$) #����#���������'����������$ "$���% !��#� ���'�������#��$ "�������l����l0�/���3����!���5)$�#

2������*������� �/.

1. %�������� ��tl"��������!��vw��3'����!��#� )".��(��x 2. %�������� ��tl"��������!��vw��3'��$���% ��23)".��(��x 3. %�������#�� ����'����vw��3'����!��#� &����(���#�"�# 4. %�������#�� ����'����vw��3'��$���% ��23&����(���#�"�#