For Java Problem 2 Write a program that reads a sequence of integer inputs all o
ID: 675100 • Letter: F
Question
For Java
Problem 2
Write a program that reads a sequence of integer inputs all on one line and prints:
The smallest and largest of the inputs
The number of odd and even inputs
Cumulative totals. If the input is 1 7 2 9, the program prints 1 8 10 19.
All adjacent duplicates. If the input is 1 3 3 4 5 5 6 6 6 2, the output is 3 5 6. Bonus for those who account for multiple adjacent numbers like 6 6 6.
If you could please show output to , that way I know when I try it, that it should print the same thing . Thank you
Explanation / Answer
import java.util.Scanner; public class P4_02B { public static void main(String[] args) { Scanner input = new Scanner(System.in); int odd_numbers = 0; int even_numbers = 0; System.out.print("Number: "); while (input.hasNextDouble()) { double input_number = input.nextDouble(); if (input_number % 2 == 0) { even_numbers += 1; } else { odd_numbers += 1; } System.out.print("Number: "); } input.close(); System.out.println("Odd numbers: " + odd_numbers); System.out.println("Even numbers: " + even_numbers); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.