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 Java program that calculates the average rainfall for three months. The program should ask...

    Write a Java program that calculates the average rainfall for three months. The program should ask the user to enter the name of each month such as June or July, and the amount of rain (in inches) that fell each month. The program should display a message similar to the following: The average rainfall for June, July, and August is 6.72 inches

  • 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 program to find the sum of all odd numbers recursively less than or equal...

    Write a program to find the sum of all odd numbers recursively less than or equal to n starting from 1, where n is any positive number. So for example n is 11 find sum of the following numbers (1,3,5,7,9,11) Write this in java please

  • •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 Java program to prompt for inputting an integer N, then enter N integers (a...

    Write a Java program to prompt for inputting an integer N, then enter N integers (a loop is needed), print the numbers user entered, and the amount of even and odd numbers (zero is even number). (1) Prompt for the user to input an integer and output the integer. (1 pts) Enter an integer: You entered: 5 (2) Prompt for the user to input N integers, output the numbers entered and the amount of even and odd numbers (9 pts)...

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

  • a. write a program that calculates the sum of two numbers x and y and then...

    a. write a program that calculates the sum of two numbers x and y and then the program should call the function by address and prints the sum b. write a program that stores 10 integers provided at run time in an array. the program should prompt the user for a random integer exit in the array, the value must be replaced by a-1

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