Question

write a java program that calculates the average of n odd numbers, where the value of...

write a java program that calculates the average of n odd numbers, where the value of n is given by the user.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//AverageNOdds.java
import java.util.Scanner;
public class AverageNOdds {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n;

        System.out.print("Enter value for n: ");
        n = scan.nextInt();

        int count = 0, sum = 0;
        for(int i = 1;count<n;i+=2){
            sum += i;
            count++;
        }

        System.out.println("Sum: "+sum);
    }
}

Output:

Enter value for n: 5
Sum: 25

Add a comment
Know the answer?
Add Answer to:
write a java program that calculates the average of n odd numbers, where the value of...
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 a Java program that generates 1000 random integers, * calculates the average and prints two...

    /*Write a Java program that generates 1000 random integers, * calculates the average and prints two counts, one for the * numbers lower than the average and one for the numbers larger than the average. */ Use java thanks

  • Write a Python program that calculates the product of all odd numbers between 1 and 20...

    Write a Python program that calculates the product of all odd numbers between 1 and 20 (1x3x5x7x9x11x13x15x17x19), using the for loop and the range function.

  • 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 java program to read 10 numbers and print them in reverse order. Modify your...

    •Write a java program to read 10 numbers and print them in reverse order. Modify your program to work for N numbers given by the user Extend your program to find the average of these numbers Extend your program to find the smallest number of these numbers Extend your program to find the largest number of these numbers •Write a program to read 10 numbers and print them in reverse order. Modify your program to work for N numbers given...

  • Write a small JAVA program that continually reads in user values and calculates the average of all values. The program s...

    Write a small JAVA program that continually reads in user values and calculates the average of all values. The program should first ask the user for the number of values to be entered. Upon storing this value, the program should proceed to read in that amount of values. What will be the best strategy here? a suggestion would be only calculating the average after you know you have read in all values. You can expect to use some type of...

  • Write a complete java program that prompts the user for their name and two numbers. The...

    Write a complete java program that prompts the user for their name and two numbers. The correct java methods and parameters must be passed to find the length of the name entered and return “TRUE” if the sum of numbers is even, or FALSE if the sum of numbers is odd: Notes included in the program Sample Program Please enter a name and I will tell you the length of the name entered John Then length of the name John...

  • ***Please use java code for the question below*** Write a program that calculates the future value...

    ***Please use java code for the question below*** Write a program that calculates the future value of a given investment at a given interest rate for a specified number of years. The formula for this calculation is: value = investmentAmount * (1 + monthly interest rate)years*12 Use text fields for the user to enter the numbers (Investment Amount, Number of Years, and Annual Interest). To get/load data from the textbox, (in this case doubles) use the following structure: (bold are...

  • Write a program that calculates the average of a stream of non-negative numbers. The user can...

    Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, zero counts as a number that goes into the average. Of course, the negative number should not be part of the average (and, for this program, the average of 0 numbers is 0). You must use a method to read...

  • 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. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • 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