117k views
3 votes
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.

1 Answer

4 votes

Answer:

The solution is given using Java

Step-by-step explanation:

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);

}

}

User Javier P
by
5.4k points