Question

In: Computer Science

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:

  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

Solutions

Expert Solution

Required program in java -->

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   int sum=0; // declare variable sum and initialize it with 0
System.out.println("Start Value: "); // ask user for start value
int start = scanner.nextInt(); // read input from user and store the value in start
System.out.println("End Value: "); // ask user for end value
int end = scanner.nextInt(); // read input from user and store the value in end
if(start<end && start>=0 && start<=50 && end>=0 && end<=50){ // if all the conditions satisfies
for(int i=start; i<=end; i++){ // for loop executes from i= start to i=end
sum=sum+i; // value of i is added in sum
}
System.out.println("Sum : "+sum); //print "Sum : " (value of sum)
   }
   }
}


Related Solutions

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: Program title/description Ask the user to input a start value in the range 0 to 50. Remember to create the...
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....
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 a C++ program that inputs three integers in the range [1..13] that represent a hand...
Write a C++ 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...
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 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 MIPS program using the Bubble Sort algorithm, that sorts an input list of integers...
Write a MIPS program using the Bubble Sort algorithm, that sorts an input list of integers by repeatedly calling a “swap” subroutine. The original unsorted list of integers should be received from the keyboard input. Your program should first prompt the user “Please input an integer for the number of elements:”. After the user enters a number and return, your program outputs message “Now input each element and then a return:”. For example, if the user enters 5 as the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT