Question

In java, write a program that gets 10 integer numbers from the user using user input,...

In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.  

Program Requirements:

Write the program in three versions with three loops. Put all three loops in the main method of your source code.

  1. version1:  use a while loop.
  2. version2:  use a do-while loop.
  3. version 3:  use a for loop.

For each version, use a loop to input 10 int numbers from the user and calculate the sum.  Then display the sum on the console window.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

VERSION 1: Using For loop

import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
//sum to store total sum of 10 integers
int sum=0,num;
  
System.out.println("Enter 10 integers : ");
  
//For loop
for(int i=0;i<10;i++){
//input of num
num = sc.nextInt();
  
//adding result to sum
sum = sum + num;

}
  
//printing sum
System.out.println("Sum = "+sum);
   }
}
VERSION 1: Using While loop

import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
//sum to store total sum of 10 integers
int sum=0,num;
  
System.out.println("Enter 10 integers : ");
  
//to count the number of integers input
int i=0;
  
//while loop
while(i<10){
//input of num
num = sc.nextInt();
  
//adding result to sum
sum = sum + num;
  
i++;
}
  
//printing sum
System.out.println("Sum = "+sum);
   }
}
VERSION 1: Using Do While loop

import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
  
//sum to store total sum of 10 integers
int sum=0,num;
  
System.out.println("Enter 10 integers : ");
  
//to count the number of integers input
int i=0;
  
//Do while loop
do{
//input of num
num = sc.nextInt();
  
//adding result to sum
sum = sum + num;
  
i++;
  
}while(i<10);
  
//printing sum
System.out.println("Sum = "+sum);
   }
}

OUTPUT:

Enter 10 integers: 1 2 3 4 5 67 8 9 Sum 45 . Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
In java, write a program that gets 10 integer numbers from the user using user input,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using java Sample Outputs Write a program which asks the user for an integer and then...

    Using java Sample Outputs Write a program which asks the user for an integer and then prints the following patterns based on that integer. Input Validation For Pattern 1, the input must be a value between 1 and 999 For Pattern 2, the input must be a value between 1 and 26. Requirements: For Pattern 1: must be able to work with up to three digit numbers. You will want to use printf to get the spacing correct. For Pattern...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Write a Java program to meet the following requirements: 1. Prompt the user to enter three...

    Write a Java program to meet the following requirements: 1. Prompt the user to enter three strings by using nextLine(). Space can be part of the string). ( Note: your program requires using loop: for-loop, while-loop or do-while-loop to get the input String ) 2. Write a method with an input variable (string type).The method should return the number of lowercase letters of the input variable. 3. Get the number of the lowercase letters for each user input string by...

  • 21 Write a program that asks the user to input the length and breadth of a...

    21 Write a program that asks the user to input the length and breadth of a soccer field, and then computes and returns the number of square meters of grass required to cover the field The formula for the area is the product of length and breadth (5) 22 The following program uses the break statement to terminate an infinite while loop to print 5 numbers Rewrite the program to use a while loop to display numbers from 1 to...

  • Write a java program that will print if n numbers that the user will input are...

    Write a java program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times asking for an integer...

  • Write a program that receives a positive integer n from the user and then calculates the...

    Write a program that receives a positive integer n from the user and then calculates the sum below by using a loop. Your program needs to ask the user to re-enter a different number if the user enters a non-positive one. Display the result. 1 SUM 1 2 3 ... n x x n = = = + + + +

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT