{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lab04_sample_while

# Lab04_sample_while - CMPSC 101 Introduction to Algorithmic...

This preview shows pages 1–4. Sign up to view the full content.

CMPSC 101 Introduction to Algorithmic Processes Summer 2007 LAB 4 – while Loop 1. Write a program that will add up the numbers from 1 to n (n is a user input). // File name: Lab04_01.cpp // Description: add up the numbers from 1 to n // Lab 04-1 // Author: Jaehyun Lim // Date: Monday, June 25, 2007 #include <iostream> using namespace std; int main() { int counter; int sum=0; cout << "Enter a number you want to add up to: " ; cin >> counter; while (counter > 0) { sum += counter; counter--; } cout << "sum = " << sum << endl; return 0; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2. Write a program that reads in numbers ten times from the user. Your program should then calculate and display their sum and average. // File name: Lab04_02.cpp // Description: calculate sum and average of 10 inputs // Lab 04-2 // Author: Jaehyun Lim // Date: Monday, June 25, 2007 #include <iostream> using namespace std; int main() { int count=10; double number; double sum=0; while (count > 0) { cout << "Please enter a number: " ; cin >> number; sum += number; count--; } cout << "sum = " << sum << endl; cout << "average = " << sum/10 << endl; return 0; }
3. Write a program that creates the following output using a while loop.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}