Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) Write and run a Java program using a while repetition structure that will ad

ID: 3555703 • Letter: #

Question

(a) Write and run a Java program using a while repetition structure that will add a series of numbers (integers) with the following parameters:

Starting number is the variable FIRST (inclusive)

Ending number is the variable LAST (inclusive or exclusive, depending on DELTA))

Interval between numbers is the variable DELTA (integer)

Sum of the integers is SUM (integer)

Assume all integers are positive and LAST > FIRST.

Test your program with the following data:

                FIRST     LAST      DELTA

1         100         1

0         100         2

0           10         5

(b) Write a second Java program for the above problem using a for loop

Explanation / Answer

A while loop is not the best method for this program simply because the variable DELTA makes a while loop more complicated, as opposed to a for loop which already has the structure for both simplistic and complex input.

#include <iostream>
using namespace std;

int main() {
int FIRST = 0;
int LAST = 0;
int DELTA = 1;
int SUM = 0;
cout >> "FIRST LAST DELTA" << endl;
//Inputted variables separated by a space
cin << FIRST << LAST << DELTA;

for(int i = FIRST; i <= LAST; i+=DELTA) {
SUM += i;
}
cout << "The Sum is " << SUM;
}