UMBC CS 201, Fall 07
UMBC CMSC 201
Fall '07

CSEE | 201 | 201 F'07 | lectures | news | help

The while loop


    while (condition)
    {
        ...statements...
    }

Example problem

Write a program to add a list of numbers entered at the keyboard. The end of the input is indicated by entering 0 as a sentinel value.

Program

/***************************************** ** File: addlist.c ** Author: Sue Bogar ** Date: 2/2/98 ** Section: 101 ** E-Mail: bogar@cs.umbc.edu ** ** This program adds a list of numbers. The end of ** the input is indicated by entering 0 as a ** sentinel value. A priming read is necessary to ** gain entry into the loop. *****************************************/ #include <stdio.h> int main() { int value, total; /* Give directions to user */ printf("This program adds a list of numbers.\n"); printf("Signal end of list with a 0.\n"); /* Initialize total */ total = 0; /* Get the first integer from the user ** This is known as a "priming read" */ printf("Enter an integer, 0 to end : "); scanf("%d", &value); /* Continue to get integers from the user ** until a 0 is entered, accumulating into total */ while (value != 0) { total = total + value; printf("Enter an integer, 0 to end : "); scanf("%d", &value); } /* Print the total */ printf("The total is %d\n", total); return 0; }

Output

linux1[88] % a.out This program adds a list of numbers. Signal end of list with a 0. Enter an integer, 0 to end : 1 Enter an integer, 0 to end : 2 Enter an integer, 0 to end : 3 Enter an integer, 0 to end : 4 Enter an integer, 0 to end : 5 Enter an integer, 0 to end : 6 Enter an integer, 0 to end : 7 Enter an integer, 0 to end : 8 Enter an integer, 0 to end : 9 Enter an integer, 0 to end : 10 Enter an integer, 0 to end : 0 The total is 55 linux1[89] %

Sum of Digits

The Task

Read in a number from the keyboard and add up the digits it contains.

The Program

/***************************************** * File: digitsum.c * Author: S. Bogar * Date: 3/9/97 * Section 101 * E-Mail:bogar@cs.umbc.edu * * This program counts the number of digits in * a positive integer. The program depends on * the fact that the last digit of a integer n * is given by n % 10 and the number consisting * of all but the last digit is given by the * expression n / 10. *****************************************/ #include <stdio.h> int main() { int n, dsum; /* Print messages to and get input from user */ printf("Sum the digits in an integer.\n"); printf("Enter a positive integer: "); scanf("%d", &n); /* Initialize dsum to 0 */ dsum = 0; /* This loop repeatedly strips the last ** digit off of n and adds it to dsum until ** there are no more digits */ while (n > 0) { /* Add one's digit to dsum */ dsum += n % 10; /* strip off the one's digit */ n /= 10; } /* Print the sum of the digits */ printf("The sum of the digits is %d\n\n", dsum); return 0; }

The Sample Run

linux1[90] % a.out Sum the digits in an integer. Enter a positive integer: 342 The sum of the digits is 9 linux1[91] % a.out Sum the digits in an integer. Enter a positive integer: -12 The sum of the digits is 0 linux1[92] %


A More Robust Digitsum

The Task

Read in a number from the keyboard, making sure that it is a positive integer, and add up the digits it contains.

The Program

/***************************************** Program: robust.c Author: S. Bogar Date: 3/9/97 Section: 101 EMail: bogar@cs.umbc.edu A more robust version of digitsum.c that verifies that the user has input a POSTIVE integer *****************************************/ #include <stdio.h> int main() { /* Declare variables */ int n, dsum; /* Print purpose of program */ printf("Sum the digits in an integer.\n"); /* Get input from the user */ printf("Enter a positive integer: "); scanf("%d", &n); /* Error checking of user input : ** This loop assures that a positive ** integer was input by printing an error ** message and asking for new input. */ while (n <= 0) { printf("Positive integers only, please.\n"); printf("Enter a positive integer: "); scanf("%d", &n); } /* Initialize dsum to 0 */ dsum = 0; /* This loop adds the last digit to dsum ** and then strips away the last digit until ** no digits remain */ while (n > 0) { dsum += n % 10; n /= 10; } /* Print the sum of the digits */ printf("The sum of the digits is %d\n\n", dsum); return 0; }

The Sample Run

linux1[93] % a.out Sum the digits in an integer. Enter a positive integer: -12 Positive integers only, please. Enter a positive integer: 42 The sum of the digits is 6 linux1[94] % a.out Sum the digits in an integer. Enter a positive integer: -32 Positive integers only, please. Enter a positive integer: -0 Positive integers only, please. Enter a positive integer: -20 Positive integers only, please. Enter a positive integer: 21 The sum of the digits is 3 linux1[95] %

The Lesson


CSEE | 201 | 201 F'07 | lectures | news | help

Thursday, 23-Aug-2007 12:00:52 EDT