L-3 MCS 260 Fri 24 Aug 2001

Below are listings for programs we discussed in class.
/* L-3 MCS 260 Fri 24 Aug 2001 : greatest common divisor */

#include <stdio.h>

int main(void)
{
   int a,b,r;

   printf("Give a two positive integers, in decreasing order : ");

   scanf("%d%d", &a, &b);

   r = a % b;

   while (r > 0)
   {
      a = b;
      b = r;
      r = a % b;
   }

   printf("The greatest common divisor is %d.\n", b);
   
   return 0;
}
Our second program is another example of a while loop.
/* L-3 MCS 260 Fri 24 Aug 2001 : put spaces in word

   The program below takes a word on input, say "hello",
   and prints the word with one space after every character,
   e.g.: "h e l l o ".  Under Unix, we put the input word
   in the file input, and run the program "space_word" like

      space_word < input

   We may also redirect the output by

      space_word < input > output

   In this way, the program will not require the user to type
   in any input, and will also not print anything on screen.   */

#include <stdio.h>

int main(void)
{
   char c;

   while (scanf("%c", &c) == 1)
      printf("%c ",c);
   
   return 0;
}