Question

In: Computer Science

Write a program that will sum the integers between a given range (limit your range from...

Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10

Algorithm:

  1. Program title/description
  2. Ask the user to input a start value in the range 0 to 50. Remember to create the user prompts.
  3. Ask the user to input a stop value in the range 0 to 50. (NOTE: the stop value should be greater than the start value) Remember to create the user prompts.
  4. Use a loop to iterate through the integers in the range, adding to the sum each time the loop executes.
  5. Output the start value, stop value, and the sum of the integers in between.

Test data (Use this data to test to ensure your program does the math correctly.):

  • Start value: 1
  • Stop value: 10
  • Sum: 55

#include <iostream>
using namespace std;
int main() {
   //Declaring variable start,stop and sum
   int start, stop, sum = 0;
   //Prompt input from users Start value
   cout << "Start value: ";
   //accepting Start value
   cin >> start;
   //Prompt input from user Stop value
   cout << "Stop value: ";
   //accepting stop value
   cin >> stop;
   //looping from start to stop value
   for (int i = start; i <= stop; i++)
       //adding each value with sum
       sum += i;
   //printing out the sum
   cout << "Sum: " << sum << endl;
   return 0;
}

add error checking to it.

Check the user’s start value to see if it falls within the accepted range. If it doesn’t, the program should tell the user that their input wasn’t in the accepted range and ask the user to reenter a value within the range.

Do the same thing with the stop value.

Once both values are determined to be within the accepted range, then continue with the program.

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main()

{

    //Declaring variable start,stop and sum

    int start, stop, sum = 0;

    //Prompt input from users Start value

    cout << "Start value: ";

    //accepting Start value

    cin >> start;

    // Check the user’s start value to see if it falls within the accepted range.

    while (start < 0 || start > 50)

    {

        // If it doesn’t, the program should tell the user that

        // their input wasn’t in the accepted range and

        cout << "Please enter a value from 0 to 50\n";

        // ask the user to reenter a value within the range.

        cout << "Start value: ";

        cin >> start;

    }

    //Prompt input from user Stop value

    cout << "Stop value: ";

    //accepting stop value

    cin >> stop;

    // Check the user’s stop value to see if it falls within the accepted range.

    while (stop < 0 || stop > 50 || start > stop)

    {

        // If it doesn’t, the program should tell the user that

        // their input wasn’t in the accepted range and

        cout << "Please enter a value from " << start << " to 50\n";

        // ask the user to reenter a value within the range.

        cout << "Stop value: ";

        cin >> stop;

    }

    // Once both values are determined to be within the accepted range

    //looping from start to stop value

    for (int i = start; i <= stop; i++)

        //adding each value with sum

        sum += i;

    //printing out the sum

    cout << "Sum: " << sum << endl;

    return 0;

}

.

Output:

.


Related Solutions

Program – version 1: Sum of Range Algorithm Write a program that will sum the integers...
Program – version 1: Sum of Range Algorithm Write a program that will sum the integers between a given range (limit your range from 0 to 50). For example, if the user want to add the integers between (and including) 1 and 10, then the program should add: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 Algorithm: Program title/description Ask the user to input a start value in the...
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
Program Name: Divisible. Write a program that calculates the number of integers in a range from...
Program Name: Divisible. Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible...
Write MIPs program that will read two integers from the user and compute for the sum...
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Write a Java program to find the sum of all integers between 200 to 250 which...
Write a Java program to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the reverse order. Using a ListItreator output the contents of the LinkedList in the original order.
Write a Console Java program that inserts 25 random integers in the range of 0 to...
Write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package. SecureRandom rand = new SecureRandom(); - creates the random number object rand.nextInt(100) - generates random integers in the 0 to 100 range) Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.
Write a program that inputs three integers in the range [1..13] that represent a hand of...
Write a program that inputs three integers in the range [1..13] that represent a hand of three cards. Your program should output an English evaluation of the hand. In the output, cards will be described as follows: - 2–10 are described by the words for their numeric values: two, three, etc. - 1 is called an ace, 11 is called a jack, 12 is called a queen, and 13 is called a king. The evaluation of a hand is based...
Question 1: Write a program to receive two integers as user input and then print: Sum...
Question 1: Write a program to receive two integers as user input and then print: Sum Difference Product Average Maximum of the two Minimum of the two You may use the min and max functions declared in the math class. ********************************************************************************** Question 2: An online bank wants you to create a program that will show a prospective customer, how the deposit will grow. Your program should read the initial balance and the annual interest rate. Interest rate is compounded monthly....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT