Tuesday, July 9, 2013

                                                  C PROGRAMS

Hello world program
/* hello.c -- The most famous program of them all ..
 */

#include <stdio.h>

int main(void) {
  printf("Hello World!\n");
  // return 0; 
}

Computing powers of 2
/* power2.c -- Print out powers of 2: 1, 2, 4, 8, .. up to 2^N
 */

#include <stdio.h>
#define N 16

int main(void) {
  int n;           /* The current exponent */
  int val = 1;     /* The current power of 2  */

  printf("\t  n  \t    2^n\n");
  printf("\t================\n");
  for (n=0; n<=N; n++) {
    printf("\t%3d \t %6d\n", n, val); 
    val = 2*val;
  }
  return 0;
}

/* It prints out :

   n       2^n
 ================
   0        1
   1        2
   2        4
   3        8
   4       16
   5       32
   6       64
   7      128
   8      256
   9      512
  10     1024
  11     2048
  12     4096
  13     8192
  14    16384
  15    32768
  16    65536

*/
Printing Large Block Letters
/* homework1.c -- This is how the code for the first homework
 *                appears when we have a single block letter.
 *                In our Unix system you can compile, link,
 *                load, and run this program with the commands
 *                    % cc homework1.c
 *                    % a.out
 */

#include <stdio.h>

void blockg(void);   /*Prototype for blockg function */

int main (void) {
   printf("\n");
   blockg();
   printf("\n");
}

/* Print out the Block letter g */
void blockg(void) {
  printf("gggggg\n");
  printf("g    g\n");
  printf("g\n");
  printf("g  ggg\n");
  printf("g    g\n");
  printf("gggggg\n");
}
  
/* It prints out:

gggggg
g    g
g
g  ggg
g    g
gggggg

*/

No comments:

Post a Comment