L-2 MCS 260 Wed 22 Aug 2001

Below are listings for programs we discussed in class. The first is a variant of the second program of Lecture 1, but now with constant defined with preprocessor directives.
/* L-2 MCS 260 Wed 22 Aug 2001 :
     constants defined with preprocessor directive #define */

#include <stdio.h>

#define FT_HEIGHT  1707       /* height of Sears Tower */
#define FT_to_CM   30         /* 1 ft = 30 cm */
#define M_to_CM    100        /* 1m = 100 cm */

int main(void)
{
   int cm_height,m_height;    /* variables to convert to metric system */

   printf("Welcome to Chicago!\n");
   printf("Its tallest building is the Sears Tower:\n");
   printf("  %d ft tall (including the antennas)\n", FT_HEIGHT);

   cm_height = FT_HEIGHT * FT_to_CM;      /* convert ft to cm */
   m_height = cm_height/M_to_CM;          /* integer division truncates */
   cm_height = cm_height%M_to_CM;         /* remainder of division */

   printf("  = %d m %d cm in the metric system.\n", m_height, cm_height);
   
   return 0;
}
Our second program uses a while loop to sum numbers.
/* L-2 MCS 260 Wed 22 Aug 2001 : summing numbers */

#include <stdio.h>

int main(void)
{
   int sum = 0;
   int nb;

   printf("Give a sequence of integers,\nterminated by any character : \n");

   while (scanf("%d", &nb) == 1) sum = sum + nb;

   printf("The sum of your numbers is %d.\n", sum);
   
   return 0;
}