Question

Write a JAVA program that has a for loop that loops ten times. Each time the...

Write a JAVA program that has a for loop that loops ten times. Each time the loop occurs enter a number, if -6 is entered end the loop. Find the sum of the numbers entered. Use a for loop.

Homework Answers

Answer #1
import java.util.Scanner;

public class ForSum {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int sum = 0, num;
        for (int i = 0; i < 10; i++) {
            System.out.print("Enter a number(-6 to exit): ");
            num = in.nextInt();
            if (num == -6)
                break;
            sum += num;
        }
        System.out.println("Sum of entered numbers is " + sum);
    }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
JustPerfects.java (for Loop). In this program, you will use loops to identify perfect numbers (a number...
JustPerfects.java (for Loop). In this program, you will use loops to identify perfect numbers (a number equal to the sum of its divisors - 6 and 28 are the first two! 6 is 2 +3 + 1 and 28 is 14+7+4+2+1). Being the perfect number is quite unique. Only 4 perfect numbers exist between 1 and 10,000 and you’ve already found two! Please write a program to find the additional pair. Bonus: How many perfect numbers exist between 10,000 and...
Using for loops: - write a Java program that adds numbers between 1 to 100 and...
Using for loops: - write a Java program that adds numbers between 1 to 100 and prints the result. - write a Java program that adds odd numbers between 1 to 100 and prints the result - write a Java program that averages even numbers between 5 to 30 and prints the result
write a main module that includes a loop that will loop 5 times. With each iteration,...
write a main module that includes a loop that will loop 5 times. With each iteration, ask the user for a number, and accumulate the sum. After the loop, print the sum of the numbers.
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
Write a program that adds the sum of digits in an integer and loops until 0...
Write a program that adds the sum of digits in an integer and loops until 0 is entered to end the program C++.
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
write a java program to find the sum of natural numbers from 1 to 1000 inclusive...
write a java program to find the sum of natural numbers from 1 to 1000 inclusive (using for loop) sample output: sum=500500
In Java. Write a program that reads-in a times table-number. The program, using this table-number will...
In Java. Write a program that reads-in a times table-number. The program, using this table-number will produce the following report (as shown). The first item in the report is a number with starting value 1. Second column is the word “ X ” (representing the times symbol). Third column is the table-number (itself). Following is an equal sign “ = “ (representing a result). Last column is the result of the operation for that line or row which is the...
Summary In this lab, you add nested loops to a Java program provided. The program should...
Summary In this lab, you add nested loops to a Java program provided. The program should print the letter E. The letter E is printed using asterisks, three across and five down. Note that this program uses System.out.print("*"); to print an asterisk without a new line. Instructions Write the nested loops to control the number of rows and the number of columns that make up the letter E. In the loop body, use a nested if statement to decide when...
The Greatest and the Least of These: Write a program with a loop that lets the...
The Greatest and the Least of These: Write a program with a loop that lets the user enter a series of integers, followed by the end of a sentinel -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered. ATTACHED IS MY CODE 1. I must use a constant in my line of code, WHERE CAN I PUT A CONSTANT? 2. How can i fool...