Question

In: Computer Science

JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...

JAVA

5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result.

Enter First number> 2

Enter second number> 6

The sum is 20

Solutions

Expert Solution

Using while loop

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int first,second,sum = 0,i;
Scanner input = new Scanner(System.in);
System.out.print("Enter First number ");
first = input.nextInt();
System.out.print("Enter second number ");
second = input.nextInt();
i = first;
while(i<=second){
sum = sum + i;
i++;
}
System.out.println("The sum is "+sum);
}
}

Screenshot of the program with output

Using for loop

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
   int first,second,sum = 0,i;
   Scanner input = new Scanner(System.in);
       System.out.print("Enter First number ");
       first = input.nextInt();
       System.out.print("Enter second number ");
       second = input.nextInt();
       for(i=first;i<=second;i++){
       sum = sum + i;
       }
       System.out.println("The sum is "+sum);
   }
}

Screenshot of the program with output

NOTE: If you want to change something , please let me know through comments; I will surely revert back to you.

Please give a up vote .....
Thank you
...


Related Solutions

1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Write a Java program that prompts the user to enter a list of integer values and...
Write a Java program that prompts the user to enter a list of integer values and displays whether the list is sorted in increasing order or not. Here is a sample run. Note that the first number in the input indicates the number of the elements in the list. <Output> Enter list: 8 101516619111 The list is not sorted <End Output <Output> Enter list: 10 11344579 11 21 The list is already sorted <End Output Create a complete class for...
java language NetBeans Write a program that prompts the user to enter the weight of a...
java language NetBeans Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter...
Use Java (Geometry: point in a rectangle?) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5.  For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle. (Hint: A point is in the rectangle if its horizontal distance to (0, 0) is less than or equal to10 / 2 and its vertical...
IN JAVA Write a complete program that asks the user to enter two real numbers from...
IN JAVA Write a complete program that asks the user to enter two real numbers from the console. If both numbers are positive print the product, if both numbers are negative print the quotient, otherwise print INVALID INPUT. Use a nested if; output should be to a dialog and console; use printf to format the console output (for real numbers specify the width and number of digits after the decimal). The output must be labeled. Follow Java conventions, indent your...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0...
JAVA Language: Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you must use the method of successive division by 2 to convert the number to binary. Your main program must print out s. Example: If the user enters the number 66, your program must print out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT