Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Would you please provide the nswer of this question using C++ and the exact name of variables as mentioned in the assignment. Thank you so

Would you please provide the nswer of this question using C++ and the exact name of variables as mentioned in the assignment. Thank you so much.

Description

Write a program that computes the sum of squares for a sequence of positive whole numbers. The program asks the user to enter the starting number. Then it asks the user to enter an ending number. It computes the square for each number from the starting number to the ending number. It also calculates the sum of these squares. It displays the numbers, their squares and the sum of the squares.

If the starting number provided by the user is larger than the ending number, the program switches the starting and the ending numbers.

The program repeats the above so the user may ask the above for different starting and ending numbers during one execution of the program.

/*

Java users

*/

The program ends when the user clicks cancel in response to a prompt for the starting number.

/*

C++ users

*/

The program ends when the user enters a negative number in response to a prompt for the starting number.

Testing

Test Run

(User input is in bold).

Enter starting number:

1

Enter ending number:

5

1 squared = 1

2 squared = 4

3 squared = 9

4 squared = 16

5 squared = 25

Sum of squares = 55

Enter starting number:

3

Enter ending number:

7

3 squared = 9

4 squared = 16

5 squared = 25

6 squared = 36

7 squared = 49

Sum of squares = 135

Enter starting number:

7

Enter ending number:

3

3 squared = 9

4 squared = 16

5 squared = 25

6 squared = 36

7 squared = 49

Sum of squares = 135

Enter starting number:

Click Cancel (Java users)

-1 (C++ users)

Bye

Sample Code

Input loop

/*

Java users

*/

String in;

int startNum, endNum;

in = JOptionPane.showInputDialog( Enter start number);

while (in != null)

{

startNum = Integer.parseInt (in);

in = JOptionPane.showInputDialog( Enter end number);

endNum = Integer.parseInt (in);

//compute square of the number and sum of squares using a For loop

in = JOptionPane.showInputDialog( Enter start number);

}

/*

C++ users

*/

int num;

cout << Enter start number << endl;

cin >> startNum;

while (startNum >= 0)

{

cout << Enter end number << endl;

cin >> endNum;

//compute the square and the sum of square using a For loop.

cout << Enter start number << endl;

cin >> startNum;

}

Computing square and sum of squares using For loop

int n, nSquared, sos;

sos = 0;

for (n=startNum; n<=endNum; n++)

{

//compute nSquared

nSquared = n * n;

//compute sos

sos = sos + nSquared;

//Any additional code

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions