Lab3_sol - CSE 114 Lab 3 1. Liang page 107, programming...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 114 Lab 3 1. Liang page 107, programming exercise 3.8. ( Sorting three integers ) Write a program that sorts three integers. The integers are entered from the input dialogs and stored in variables num1 , num2 , and num3 , respectively. The program sorts the numbers so that num1 num2 num3 . public class Exercise3_8 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); // Enter three numbers System.out.print("Enter three integers: "); int num1 = input.nextInt(); int num2 = input.nextInt(); int num3 = input.nextInt(); if (num1 > num2) { int temp = num1; num1 = num2; num2 = temp; } if (num2 > num3) { int temp = num2; num2 = num3; num3 = temp; } if (num1 > num2) { int temp = num1; num1 = num2; num2 = temp; } System.out.println("The sorted numbers are " + num1 + " " + num2 + " " + num3); } } 2. Liang page 109, programming exercise 3.12. (C hecking a number ) Write a program that prompts the user to enter an integer and checks whether it is divisible by both 5 and 6 , or neither of them, or just one of them. import java.util.Scanner; public class Exercise3_12 { public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter an integer System.out.print("Enter an integer: "); int number = input.nextInt(); if (number % 5 == 0 && number % 6 == 0) System.out.println(number + " is divisible by both 5 and 6"); else if (number % 5 == 0 ^ number % 6 == 0)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
System.out.println(number + " is divisible by both 5 and 6, but not both");
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Lab3_sol - CSE 114 Lab 3 1. Liang page 107, programming...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online