**Section 4.7 /* This is an example of using printf() function */ /* These few lines are comment to the program */ /* The example is prepare by Nicholas & Vincent */ int main (void) { printf("Hi! How are you?\n"); printf("Welcome to this class.n"); return 0; } The result is: Hi! How are you? Welcome to this class. int main (void) { printf("Hi! \nHow are you?\n"); printf("Welcome to \nthis class.n"); return 0; } The result is: Hi! How are you? Welcome to this class. /* This program demonstrates the function of printf () */ int main (void) { int i = 2; int j = 3; printf("%d and %d",i,j); printf("%d and %d",j,i); return 0; } The result is: 2 and 3 3 and 2 ** Section 4.8.3 /* This program demonstrates the usage of operator */ /* The command a += b is equal to a= a+b */ int main (void) { int i,j,k; i = 2; j = 3; k = 4; i += j; printf("The first result is %d\n",i); i -= j; printf("The second result is %d\n",i); i *= j; printf("The third result is %d\n",i); i /= j; printf("The forth result is %d\n",i); i %= j; printf("The fifth result is %d\n",i); return 0; } The result are: The first result is 5 The second result is 2 The third result is 14 The forth result is 2 The fifth result is 2 ** Section 4.94 /* Demonstration of the for loop */ int main (void) { int sum, i; sum =0; printf(" i sum from 1 to i\n"); printf(" --- ---------------\n"); for (i = 1 ; i <= 5; i++) { sum = sum + 1; printf (" %d %d\n",i,sum); } return 0; } The result is: i sum from 1 to i --- --------------- 1 1 2 3 3 6 4 10 5 15 /* The same program with while loop */ int main (void) { int sum, i; sum =0; printf(" i sum from 1 to i\n"); printf(" --- ---------------\n"); while (i <= 5) { sum = sum + 1; printf (" %d %d\n",i,sum); i++; } return 0; } /* The same program with do-while loop */ int main (void) { int sum, i; sum =0; printf(" i sum from 1 to i\n"); printf(" --- ---------------\n"); do { sum = sum + 1; printf (" %d %d\n",i,sum); i++; }while(i <= 10); return 0; } /* This program demonstrate two loops */ int main (void) { int i,j,data; for ( i = 1 ; i < 5 ; i ++) { for ( j = 1 ; j < 3 ; j ++) { data = i * j; printf("%d * %d = %d ",i,j,data); } printf ("\n"); } return 0; } The result is: 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 /* The same program with two while loops */ int main (void) { int i,j,data; i = 1; while ( i < 5 ) { j = 1; while ( j < 3 ) { data = i * j; printf("%d * %d = %d ",i,j,data); j ++; } printf ("\n"); i ++; } return 0; } The result is: 1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 ** Section 4.9.2 /* < if - else > command */ /* This is a program for test odd or even number */ int main (void) { int i,temp; printf ("Input the number by key pad \n"); i = getnum (); /* The function get a number from the key pad */ temp = i % 2; /* Find the remainder */ if ( temp == 1) printf ("The number is odd \n"); else printf ("The number is even \n"); return 0; } ** Section 4.9.6 /* < Break > instruction */ /* Test the key pad */ int main (void) { int i; while (1) { printf ("Press the '2' in the key pad\n"); i = getnum(); if (i == 2) break; } printf ("The num key '2' is ok !!"); return 0; } ** Section 4.9.7 /* < Continue > instruction */ int main (void) { int i; int sum = 0; for ( i = 1 ; i < 10 ; i ++) { if ( i == 5 ) continue; sum = sum + i; } printf ("The sum is %d.",sum); return 0; } The result is: The sum is 50. ** Section 4.10 <<< On this section the LED program should be added >>> /* An example of using array */ int main (void) { int i,j,temp; int data[5] = { 95, 50, 64, 58, 80 }; for ( i = 0; i < 4; i++) { for ( i = 0; i < 4; i++) { if (data[j] > data [j+1]) { temp = data [j]; data [j] = data [j+1]; data [j+1] = temp; } } } printf ("The arranged data is:\n"); for ( i = 0; i < 5; i ++) printf ("%d\n",data [i]); return 0; } The result is: The arranged data is: 50 58 64 80 95 ** Section 4.8.5 /* The us of < or > */ int main (void) { int i = 15; int j = 18; int k; k = i | j; printf ("The bitwise or of 15 and 18 is: \n"); printf ("%d\n",k); return 0; } The result is: The bitwise or of 15 and 18 is: 31 The operation is: 00001111 00010010 ________ 00011111