Book Cover

Introduction to Scientific Programming
Computational Problem Solving Using:
Maple and C
Mathematica and C

Author:
Joseph L. Zachary
Online Resources:
Maple/C Version
Mathematica/C Version

Loops Tutorial

In this lesson you will do six exercises designed to give you experience with creating while loops.

Counting from 1 to 10 in a While Loop

Copy loop1.c into your home directory.

Modify it so that it prints out the numbers from 1 up to 10 using a while loop.

The places in the code where code needs to be inserted is marked with comments that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

When you have done this, click here and your work will be automatically checked.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

Counting from 10 down to 1 in a While Loop

Copy loop2.c into your home directory.

Modify it so that it prints out the numbers from 10 down to 1 using a while loop.

The places in the code where code needs to be inserted is marked with comments that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

When you have done this, click here and your work will be automatically checked.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

Sum of first 5 Integers

Copy loop3.c into your home directory.

Modify it so that it prints out the sum of the first 5 integers. For example, the sum of the first 3 integers is 6 (1+2+3).

Your program will also print out the current value of the sum each time through the loop. The final sum will be printed out on a line by itself. For example, if you were summing the first 3 integers rather than the first 5 integers, the output would be

Loop Section:
  After adding 1, the currnet value of sum in the loop is 1
  After adding 2, the currnet value of sum in the loop is 3
  After adding 3, the currnet value of sum in the loop is 6

Answer section:
6

The places in the code where code needs to be inserted is marked with comments that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

When you have done this, click here and your work will be automatically checked.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

Sum of N Integers

Copy loop4.c into your home directory.

Modify it so that it prints out the sum of the first n integers, where n is read from the keyboard.

For example, if n were 2 the sum of the first 2 integers would be 3 (1+2).

Your program will also print out the current value of the sum each time through the loop. The final sum will be printed out last.

The places in the code where code needs to be inserted is marked with comments that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

When you have done this, click here and your work will be automatically checked.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

Sum of Even N Integers

Copy loop5.c into your home directory.

Modify it so that it prints out the sum of the first n even integers, where n is read from the keyboard.

For example, if n were 2 the sum of the first 2 even integers would be 6 (2+4).

Your program will also print out the current value of the sum each time through the loop. The final sum will be printed out on a line by itself. For example, if n were 2 then the output would be

Please input the number of even terms to add up (an integer).

Input Section:
  Input n is: 2

Loop Section:
  In iteration 1, after adding 2, the current value of sum in the loop is 2
  In iteration 2, after adding 4, the current value of sum in the loop is 6

Answer Section:
  The answer is 6

The places in the code where code needs to be inserted is marked with comments that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

When you have done this, click here and your work will be automatically checked.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.

Maximum Sum < 100

Copy loop6.c into your home directory.

Suppose you want to teach a child about math while giving them their allowance. This month's lesson is about summation, so you give the child 1 penny on the first day of the month and then on the next day give them the same number of pennies as on the previous day, plus one. In other words, you give the child 1 penny on the first day of the month, 2 pennies on the second day, 3 pennies on the third day, 4 pennies on the fourth day, etc.

You want to remember to go to the bank on the day that the sum the child has reaches 1 and get a 1 bill to exchange for the pennies they've accumulated.

What is the first day of the month you can go get the dollar bill? In other words, what is the first day that they will have a 1 or more than 1 in change.

Modify the program so that it finds that day, and also prints out how much total change the child has on that day (it will be 100 or greater).

The places in the code where code needs to be inserted is marked with comments that explain what sort of code should be inserted. Your modifications must be limited to replacing the comments with appropriate code. In particular, do not add or remove any printf or scanf statements.

Compile, test, and modify your program until you have it right. If you get hopelessly confused, download a fresh copy and start over.

Compile and test it until you have it right.

When you have done this, click here and your work will be automatically checked.

If you did something wrong, you will be notified. Otherwise, you will be told that your program was correct.