Question

I have to write a program in java which uses while loops. 1.prompt the user to...

I have to write a program in java which uses while loops. 1.prompt the user to input two intergers: firstnum ans second numnumber. (first number must be smaller that the second number). 2. Output all the even numbers between first number and second number inclusive. 3. Output the sum of all the even number between first number and second number inclusive. 4 Output all the odd numbers between first number and second number inclusive. 5. Output the sum of all the odd numbers between first number and second number inclusive

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

The solution to the above question is given below with screenshot of output.

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

I kept the logic simple and i have tested all outputs.

If there is anything else do let me know in comments.

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

============ CODE TO COPY ===============================

main.java

 import java.util.Scanner;  // Import the Scanner class

class Main {

  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int sum_even = 0;
    int sum_odd  = 0;

    System.out.println();

    // Taking first number from user
    System.out.print("Enter first number: ");
    int first_number  = scanner.nextInt();

    // Taking second number from user
    System.out.print("Enter second number: ");
    int second_number = scanner.nextInt();
    
    // if second number if smaller ask again
    while( second_number < first_number ){

      System.out.print("Second number smaller, enter again : ");
      second_number = scanner.nextInt();

    }

    // Output all the even numbers between first number and second number inclusive
    System.out.println();
    System.out.print("Even numbers between first number and second number inclusive: ");
    for ( int i = first_number ; i <= second_number; i++){
      if ( i % 2 == 0 ){
        System.out.print(i + " ");
        sum_even += i;
      }
    }

    System.out.println();
    System.out.print("Sum of all the even number between first number and second number inclusive : ");
    System.out.print(sum_even);

    // Output all the odd numbers between first number and second number inclusive
    System.out.println();
    System.out.println();
    System.out.print("Odd numbers between first number and second number inclusive: ");
    for ( int i = first_number ; i <= second_number; i++){
      if ( i % 2 != 0 ){
        System.out.print(i + " ");
        sum_odd  += i;
      }
    }

    System.out.println();
    System.out.print("Sum of all the odd number between first number and second number inclusive :");
    System.out.println(sum_odd );

  }
}


=====================================================
Output :

java version 1.8.0_31 Java(TM) SE Runtine Environnent (build 1.8.0_31-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.31-b

Add a comment
Know the answer?
Add Answer to:
I have to write a program in java which uses while loops. 1.prompt the user to...
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 the program in VBA b. Write a program that uses a For loop to perform...

    write the program in VBA b. Write a program that uses a For loop to perform the following steps: a. Prompt the user to input two integers: firstNum and secondNum(firstNum must be less than secondNum). b. Output all odd numbers between firstNum and secondNum. c. Output the sum of all odd numbers between firstNum and secondNum. d. Output all even numbers between firstNum and second Num. e. Output the sum of all even numbers between firstNum and secondNum.

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

  • Write a program with loops that compute a.The sum of all even numbers between 2 and...

    Write a program with loops that compute a.The sum of all even numbers between 2 and 100 (inclusive). b.The sum of all squares between 1 and 100 (inclusive). c.All powers of 2 from 20 up to 220. d.The sum of all odd numbers between a and b (inclusive), where a and b are inputs. Note*: For part d, enter 3 and 21 as input Output should be: a. 2550 b. 338350 c. 1.0 2.0 4.0 8.0 ... 1048576.05 d. 120

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

  • Write a single program in java using only do/while loops for counters(do not use array pls)...

    Write a single program in java using only do/while loops for counters(do not use array pls) for the majority of the program and that asks a user to enter a integer and also asks if you have any more input (yes or no) after each input if yes cycle again, if not the program must do all the following 4 things at the end of the program once the user is finished inputting all inputs... a.The smallest and largest of...

  • Write a Java program that prompts the user for the page size used in a virtual...

    Write a Java program that prompts the user for the page size used in a virtual memory system; this will be a power of two between 512 (29) and 16384 (214), inclusive. Your program should check the user input for page size to make sure it is one of the allowable inputs (must be a power of 2 and cannot be smaller than 512 or larger than 16384), and should then prompt the user for a virtual address (assume 32-bit...

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

  • Problem 16. Write a program to prompt the user for a floating point number x and...

    Problem 16. Write a program to prompt the user for a floating point number x and compute the following formula: Problem 18. Write a program fragment that uses nested for loops to produce the following output, making sure that the user input is between 3 and 42.

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

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