0% found this document useful (0 votes)
28 views3 pages

Division by 0

The document contains code to handle division by zero and find numbers modulo 5. The division method gets two numbers from the user, checks if the second is zero and prompts again if so, then performs the division and prints the result. It exits if either number is 999. The mod method prints numbers from 0 to 100 that are 3 modulo 5.

Uploaded by

scribd_comverse
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Division by 0

The document contains code to handle division by zero and find numbers modulo 5. The division method gets two numbers from the user, checks if the second is zero and prompts again if so, then performs the division and prints the result. It exits if either number is 999. The mod method prints numbers from 0 to 100 that are 3 modulo 5.

Uploaded by

scribd_comverse
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

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;

while (fn != 999 && sn != 999) {

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;

while (sn == 0) { sn = keyBoard.nextInt();

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

System.out.print(x + " "); } }

You might also like