1 / 8

Computer Programming

Computer Programming. Lab 8. Exercise 1. ( Use the && , || and ^ operators ) Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both .

kueng
Download Presentation

Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming Lab 8

  2. Exercise 1 (Use the &&, || and ^ operators) Write a program that prompts the user to enter an integer and determines whether it is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both. Here is a sample run of this program:

  3. Source Code package Exersise1; import java.util.Scanner; public class Exersise1{ public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a number: "); int x = input.nextInt(); System.out.println(((x % 5 == 0)&& (x % 6 == 0 ))? x + " is divisible by 5 and 6? True " : x + " is divisible by 5 and 6? false "); System.out.println(((x % 5 == 0)||(x % 6 == 0 ))? x + " is divisible by 5 or 6? True " : x + " is divisible by 5 or 6? false "); System.out.println(((x % 5 == 0)^(x % 6 == 0 ))? x + " is divisible by 5 and 6 put not both? True " : x + " is divisible by 5 and 6 put not both ? false ");}}

  4. Exercise 2

  5. Source Code package Exercise2; import java.util.Scanner; public class Exercise2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); intcomputerNumber = (int)(Math.random() * 3); System.out.print("scissor (0), rock (1), paper (2): "); intuserNumber = input.nextInt(); switch (computerNumber) { case 0: if (userNumber == 0) System.out.print("The computer is scissor. You are scissor too. It is a draw"); else if (userNumber == 1) System.out.print("The computer is scissor. You are rock. You won"); else if (userNumber == 2) System.out.print("The computer is scissor. You are paper. You lost"); break;

  6. case 1: if (userNumber == 0) System.out.print("The computer is rock. You are scissor. You lost"); else if (userNumber == 1) System.out.print("The computer is rock. You are rock too. It is a draw"); else if (userNumber == 2) System.out.print("The computer is rock. You are paper. You won"); break; case 2: if (userNumber == 0) System.out.print("The computer is paper. You are scissor. You won"); else if (userNumber == 1) System.out.print("The computer is paper. You are rock. You lost"); else if (userNumber == 2) System.out.print("The computer is paper. You are paper too. It is a draw"); break;} }}

  7. Exercise 3 (Financials: currency exchange) Write a program that prompts the user to enter the exchange rate from currency in U.S. dollars toSaudi riyal SR or vice . Prompt the user to enter 0 to convert from U.S. dollars to Saudi riyal SR and 1 to convert from Saudi riyal SR and U.S. dollars. Prompt the user to enter the amount in U.S. dollars or Saudi riyal SR to convert it to Saudi riyal SR or U.S. dollars, respectively. Here are the sample runs:

  8. Source Code : package exercise3; import java.util.Scanner; public class Exercise3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("enter The amount of monye "); int amount=input.nextInt(); System.out.println("Select 0 to convert it to U.S. dollars , and 1 to convert it to Saudi riyal SR "); int choice=input.nextInt(); double result; switch (choice) { case 0: result= (amount/3.75); System.out.printf ( "%d SR equal to %f $",amount,result); break; case 1: result=(amount*3.75); System.out.printf ( "%d $ equal to %f SR",amount,result); break ; } } }

More Related