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

Info icon This 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)
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern