Question

Write a java application to input 2 positive integers X and Y such that X is...

Write a java application to input 2 positive integers X and Y such that X is less than Y.
Then the program should do the following:
1. Output the sum of even integers between 1 and X.
2. Output the average of odd integers between 1 and Y.
3. Output the common divisors of X and Y.

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

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


MyNumbers.java
======
import java.util.Scanner;

public class MyNumbers {
   public static void main(String[] args) {
       int X, Y;
       int evenSum = 0;
       double oddAverage =0 ;
       int count = 0;
       Scanner in = new Scanner(System.in);

       System.out.print("Enter X: ");
       X = in.nextInt();
      
       System.out.print("Enter Y: ");
       Y = in.nextInt();
      
       for(int i = 1; i <= X; i ++) {
           if(i % 2 == 0)
               evenSum += i;
       }
       System.out.println("Sum of even numbers between 1 and " + X + " = " + evenSum);
      
      
       for(int i = 1; i <= Y; i++) {
           if(i % 2 == 1) {
               oddAverage += i;
               count++;
           }
       }
      
       oddAverage = oddAverage / count;
       System.out.println("Average of odd numbers between 1 and " + Y + " = " + oddAverage);
      
       System.out.println("Common diviors of " + X + " and " + Y);
       for(int i = 1; i <= X && i <= Y; i++) {
           if(X % i == 0 && Y % i == 0)
               System.out.print(i + " ");
       }
       System.out.println();
   }
}

output
====
Enter X: 12
Enter Y: 15
Sum of even numbers between 1 and 12 = 42
Average of odd numbers between 1 and 15 = 8.0
Common diviors of 12 and 15
1 3

Add a comment
Know the answer?
Add Answer to:
Write a java application to input 2 positive integers X and Y such that X is...
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
  • This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input...

    This is in Java. Write the application, TheSentinel  to read a set of integers. Stop reading input when a non-integer is read. Use this exact prompt: System.out.print("Enter an integer or Q to quit: "); Note: you will actually quit on any non-integer. Do the following things: Find and print the sum of all the even numbers Find and print the smallest of the inputs Determine if the number 7 is in the input. If 7 is in the inputs, print "7...

  • Use a java program that does the following: . (10 points) Write a program as follows...

    Use a java program that does the following: . (10 points) Write a program as follows a. Prompt the user to input two positive integers: nl and n2 (nl should be less than n2) b. If the user enters the negative number(s), convert it/them to positive number(s) c. If nl is greater than n2, swap them. d. Use a while loop to output all the even numbers between nl and n2 e. Use a while loop to output the sum...

  • IN JAVA Write a program that first gets a list of integers from input. The input...

    IN JAVA Write a program that first gets a list of integers from input. The input begins with an integer indicating the number of integers that follow. Then, get the last value from the input, which indicates a threshold. Output all integers less than or equal to that last threshold value. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 50 60 140 200 75 100 the output is: 50 60 75...

  • 1. Write a for loop that prints the sum of all positive even integers less than...

    1. Write a for loop that prints the sum of all positive even integers less than 200. Assume that variables i and sum are already declared. 2. Write a while loop that prints the sum of all positive odd integers less than 100. Assume that variables i and sum are already declared. 3. Write a do-while loop that prints the sum of all positive multiples of 3 less than or equal to 150. Assume that variables i and sum are...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • Write a function that takes an array of integers as an argument and returns a value...

    Write a function that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers. The function should return X – Y The signature of the function is: int f(int[ ] a) Examples if input array is return {1} 1 {1, 2} -1 {1,...

  • A java program Write a program that prompts for and reads in a positive    integer...

    A java program Write a program that prompts for and reads in a positive    integer into a variable n. Your program should then sum    the first n ODD integers and display the sum. For    example, if 3 is entered for n, your program should display    the the sum 1 + 3 + 5. If 5 is entered, your program should    display the sum 1 + 3 + 5 + 7 + 9. What is the...

  • 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 program that first gets a list of integers from the input and adds them...

    Write a program that first gets a list of integers from the input and adds them to an array. The input begins with an integer indicating the number of integers that follow (Your program should work for any size of the array). Then, get the last value from the input, and output all integers less than or equal to that value by iterating through the array. Ex: If the input is: Enter the number of integers: 5 Enter integer 1:...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

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