Question

JAVA Write an application that will input four positive integers from the user. The program should...

JAVA Write an application that will input four positive integers from the user. The program should print out the largest and smallest number. It should print how many of the numbers are even and how many are odd. It should print how many of the numbers are one digit and how many of the numbers are two digit.

It should print out the sum of all even numbers and the sum of all odd numbers. It should print out the sum of all one digit numbers and the sum of all two digit numbers.

You do not need to do any error checking to make sure that the numbers are positive or one or two digit. You may not use arrays or loops for this lab.

Homework Answers

Answer #1

PROGRAM

import java.util.Scanner;

public class Numbers {

   public static void main(String[] args) {
       // Create scanner object to help take user input
       Scanner sc = new Scanner(System.in);
      
       //Variables to store different sums
       int sumEven=0,sumOdd=0,sumOneDigit=0,sumTwoDigit=0;
      
       //Prompt message to user and store it in variables
       System.out.println("Enter four positive integers: ");
       int num1 = sc.nextInt();
       int num2 = sc.nextInt();
       int num3 = sc.nextInt();
       int num4 = sc.nextInt();
      
       //Find and print Largest number
       int largest = num1;
       if(num2>largest)
           largest=num2;
       if(num3>largest)
           largest=num3;
       if(num4>largest)
           largest=num4;
       System.out.println("The largest number is: "+largest);
      
       //Find and print smallest number
               int smallest = num1;
               if(num2<smallest)
                   smallest=num2;
               if(num3<smallest)
                   smallest=num3;
               if(num4<smallest)
                   smallest=num4;
               System.out.println("The smallest number is: "+smallest);
              
       //Count and print number of even and odd numbers
       //Also find sum of even and odd numbers
       int oddCount=0,evenCount=0;
       if(num1%2 == 0) {
           evenCount++;
           sumEven = sumEven + num1;
       }
       else {
           oddCount++;
           sumOdd = sumOdd + num1;
       }
      
       if(num2%2 == 0){
           evenCount++;
           sumEven = sumEven + num2;
       }
       else{
           oddCount++;
           sumOdd = sumOdd + num2;
       }
      
       if(num3%2 == 0){
           evenCount++;
           sumEven = sumEven + num3;
       }
       else{
           oddCount++;
           sumOdd = sumOdd + num3;
       }
      
       if(num4%2 == 0){
           evenCount++;
           sumEven = sumEven + num4;
       }
       else{
           oddCount++;
           sumOdd = sumOdd + num4;
       }
      
       System.out.println("Number of even numbers: "+evenCount);
       System.out.println("Number of odd numbers: "+oddCount);
      
       //Count and print number of one digit and two digit numbers
       //Also find sum of even and odd numbers
       int oneDigitCount=0,twoDigitCount=0;
       if((Integer.toString(num1)).length()==1) {
           oneDigitCount++;
           sumOneDigit = sumOneDigit +num1;
       }
       else if((Integer.toString(num1)).length()==2) {
           twoDigitCount++;
           sumTwoDigit = sumTwoDigit + num1;
       }
          
       if((Integer.toString(num2)).length()==1){
           oneDigitCount++;
           sumOneDigit = sumOneDigit +num2;
       }
       else if((Integer.toString(num2)).length()==2){
           twoDigitCount++;
           sumTwoDigit = sumTwoDigit + num2;
       }
      
       if((Integer.toString(num3)).length()==1){
           oneDigitCount++;
           sumOneDigit = sumOneDigit +num3;
       }
       else if((Integer.toString(num3)).length()==2){
           twoDigitCount++;
           sumTwoDigit = sumTwoDigit + num3;
       }
      
       if((Integer.toString(num4)).length()==1){
           oneDigitCount++;
           sumOneDigit = sumOneDigit +num4;
       }
       else if((Integer.toString(num4)).length()==2){
           twoDigitCount++;
           sumTwoDigit = sumTwoDigit + num4;
       }
      
       //Print the results
       System.out.println("Number of one digit numbers: "+oneDigitCount);
       System.out.println("Number of two digit numbers: "+twoDigitCount);
      
       System.out.println("Sum of even numbers: "+sumEven);
       System.out.println("Sum of odd numbers: "+sumOdd);
       System.out.println("Sum of one digit numbers: "+sumOneDigit);
       System.out.println("Sum of two digit numbers: "+sumTwoDigit);
   }

}

OUTPUT SCREENSHOT

Know the answer?
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
Similar Questions
The Java program should sum the odd integers from 1 to the integer that the user...
The Java program should sum the odd integers from 1 to the integer that the user enters. For example, if the user enters 30, the program should sum the odd integers from 1 to 30.   Print "Please enter a positive nonzero integer." Declare a Scanner and obtain an integer from the user.   Use a Do...while statement and calculate the sum Use the integer that the obtained from the previous step for the loop-continuation condition. Display the sum Print the sum...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Note: There is a white space in between the numbers. Java programming please answer ASAP before 2:30
Write a C++ program to read a positive integer greater than 0, the program should compute...
Write a C++ program to read a positive integer greater than 0, the program should compute and display the following: - Sum of odd digits in the number - Count of even digits in the number - The smallest digit.                    For example, if the input is 24536, then Sum of odd digits in the number = 8 Count of even digits in the number = 3 Smallest digit = 2
Write a program that takes n integer numbers from the user, and then counts the number...
Write a program that takes n integer numbers from the user, and then counts the number of even numbers and odd numbers and print them to the screen. Sample Output: Enter how many numbers you have: 10 Enter the 10 numbers: 1 3 19 50 4 10 75 20 68 100 The number of even numbers is: 6 The number of odd numbers is: 4 (( in java ))
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...
In C++ write a program that prompts the user to enter two positive integers less than...
In C++ write a program that prompts the user to enter two positive integers less than 1,000,000,000 and the program outputs the sum of all the prime numbers between the two integers. Two prime numbers are called twin primes, if the difference between the two primes is 2 or -2. Have a program output all the twin primes and the number of twin primes between the two integers. NEEDED OUTPUTS FOR PROGRAM: Enter two positive integers <1,000,000,000: 1 100 -1...
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
Write a complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
Write a C++ fragment that prompts the user to enter integers one at a time, keeping...
Write a C++ fragment that prompts the user to enter integers one at a time, keeping track of the total number of odd numbers entered. If a negative number is entered, stop asking for numbers and print the result. Assume all needed headers are included.
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...