Division by 0
Division by 0
//Create division method to accept two numbers from the user to check if it's a division by zero. // If it is zero, display error message and ask user to input another number. If the second number is not zero, perform the division. If 999 is entered as either number exit program. public static void division() { //Initialize imported scanner utility. Scanner keyBoard = new Scanner(System.in);
int fn = 0; int sn = 0;
System.out.print("Enter the first number (Type 999 to EXIT): "); fn = keyBoard.nextInt(); if (fn == 999) break; System.out.print("Enter the second number (Type 999 to EXIT: "); sn = 0;
if (sn == 0) System.out.print("Error: division by zero. Please enter the second number again: "); } if (fn != 999 && sn != 999) { double result = (double) fn / sn; System.out.printf( "The first number " + fn + " divided by the second number " + sn + " is %.2f", result); System.out.println();
} } }
MOD
//Create mod method and examine each number from 0 to 100 when mod 5 is applied and when it equals 3, print the number. public static void mod() { System.out.println(); System.out.println("These are the numbers from 0 to 100 who when mod 5 is applied equal 3"); for ( int x = 0; x <= 100; x++ ) {
if ( x % 5 == 3 ) {