Question
java programming!!!
Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instanti
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student,

below i have written the complete Java program as per the requirement.

==============================================================

Program:

==============================================================

import java.util.Scanner;

public class Sum

{

   public static void main(String args[])

   {

       //variable declaration

       int num;

       double sum = 0;

       Scanner in = new Scanner(System.in);

       System.out.println("Please enter the number of elements: ");

       num = in.nextInt();

       double [] array = new double[num];

       System.out.println("Enter the " +num+" array elements: ");

       for(int i = 0; i<num; i++)
       {
           array[i] = in.nextDouble();
       }

       //calculate the sum

       for(int i = 0; i<num; i++)
       {
           sum = sum + array[i];
       }
       //display the sum
       System.out.println("The sum of the array elements is: "+sum);
   }

}


=============================================================

Sample Output:

nirmalsharma@ubuntu:~$ java Sum Please enter the number of elements: Enter the 5 array elements: 12.3 45.6 7.6 4.5 8.6 The su

=============================================================

Kindly Check and Verify Thanks..!!!

Add a comment
Know the answer?
Add Answer to:
java programming!!! Write the code fragment that will do the following: • Ask the user how...
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
  • Write Java code that does the following Ask the user to enter a file name. Ask...

    Write Java code that does the following Ask the user to enter a file name. Ask the user for two integers as the lower bound and the upper bound. Use a loop to write all the odd numbers between the lower bound and the upper bound (inclusive) to the file, and then close the file. Use either while loop, do while loop, or for loop to complete the program. PrintWriter class is used. The file is closed before the program...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • write a program code that asks the user how many numbers they wish to find the...

    write a program code that asks the user how many numbers they wish to find the statistics of and then asks the user to enter those numbers. The program must then calculate and display the average variance and standard deviation of the numbers entered by the user. the program code must -ask three user how many numbers they wish to find the statistics of -ask the user to enter those numbers and store them in an array - use a...

  • Ask the user to enter a message Ask the user how many time they want to...

    Ask the user to enter a message Ask the user how many time they want to print the message Then use "while","do-while" and "for" loop to print the message. Show the number at the beginning of the lines when printing. For example: Enter your message: Welcome to Java How many time you want to print this message: 3 Using "while" loop: 1=> Welcome to Java 2=> Welcome to Java 3=> Welcome to Java Using "do-while" loop: 1=> Welcome to Java...

  • (java) Write a While loop that prompts the user for only even numbers. Keep prompting the...

    (java) Write a While loop that prompts the user for only even numbers. Keep prompting the user to enter even numbers until the user enters an odd number. After the loop ends, print the sum of the numbers. Do not include that last odd number. You should not store the numbers in an array, just keep track of the sum. Make sure to use a break statement in your code. You may assume that a java.util.Scanner object named input has...

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • 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....

  • Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a...

    Java Code Help! Code this: Calculate statistics. Write a class called Statistics that can calculate a number of properties about an array of doubles. There should be separate static methods to calculate the min, the max, the mean, the median, the standard deviation (make sure to make use of your mean method to implement this), and the mode. For the mode, you can assume that the data set is not multimodal. This class should not have a main method. Write...

  • Please complete this code for java Lab Write a program as follows: Create an array with...

    Please complete this code for java Lab Write a program as follows: Create an array with 10 elements. The array should contain numbers generated randomly between 1 and 100 Ask the user to enter any number - Search the array to see if the user entered number is in the array If the user-entered number is in the array, print a 'match found' message. Also print at which index the match was found, else print a 'match not found' message...

  • C programming Ask the user how many numbers they would like generated. Your program will then...

    C programming Ask the user how many numbers they would like generated. Your program will then generate that many numbers (between 0 and 1000, inclusive) into an array of the same size. Find the smallest and largest numbers in the array, printing out the values and locations of those numbers in the array. Then, find the sum and average of the numbers in the array. Finally, print out the entire array to verify. You MUST use functions where appropriate!

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