# Assignment 1: Counters and Accumulators

## Overall program description

Ok, so you are not going to become a facebook/google style billionaire selling this program to some rich investors, but for your first program it will give you some exercise with the basics... which of course, is a very good place to start.

In this program you will practice several of the concepts we have studied:

• creating and using counters and accumulators.
• a loop
• user input from the keyboard

The first step in your program will be to get a number from the user. You will use a "prompt" statement to do this (remember to do the "safe" prompt which converts the text to a number). Ask the user for an integer to be an upper limit (see "Points for Thought" below). Your program will then produce several computations from the sequence of integers from 1 to whatever upper limit the user has given.

You will compute these values:

• the sum of the integers from 1 to the upper limit (including the upper limit)
• the square root of the sum of all integers in the sequence
• the sum of the floor of the square root of each integers in the sequence (remember the floor is the integer part of a number with decimal places)
• the product of all the integers in the sequence
• the average of all the integers in the sequence

Each result you compute will be reported in an "alert" statement.

## Examples

Let's say the user gives us the integer 4 as upper limit. The program will then work with the integer sequence 1, 2, 3, 4. The program will compute these values:
• sum of the integers: 1+2+3+4 produces 10
• square root of the sum of the integers: Math.sqrt(10) produces 3.1622776601683795
• sum of the floor of the square root of each integer:
Math.floor(Math.sqrt(1))+Math.floor(Math.sqrt(2))+
Math.floor(Math.sqrt(3))+Math.floor(Math.sqrt(4)) produces 1+1+1+2 is 5
• product of the integers: 1*2*3*4 produces 24
• average of the integers: (1+2+3+4) / 4 produces 10/4 which is 2.5

## Hints

You can use a "for" loop to generate the integers in the sequence, one at a time. The loop index will give you the current integers in the sequence. Remember, the loop index counts how many times the loops body has been executed.

Use a accumulator inside the loop body to build up the sum of all the integers. Use another accumulator in the body similarly to build up the product. Use yet another accumulator in the body to build up the sum of the floor of the square root; this building will require calling the Math functions "floor" and "sqrt" correctly.

You can use a counter in the loop body to count how many integers are in the sequence (since you will have to divide the sum by the number of integers to get the average). However, you can also recognize that since we start the sequence at 1, the upper limit tells directly how many integers are in the sequence.

## Points for Thought

How might you check to be sure the number the user gives as upper limit is actually an integer (as opposed to something like 13.75)?