-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdivision.java
More file actions
26 lines (23 loc) · 776 Bytes
/
Copy pathdivision.java
File metadata and controls
26 lines (23 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//write a program in java to divide two numbers where you should be checking the condition of divide by 0 error.
import java.util.Scanner;
public class division {
static int divide(int a, int b) {
if (b == 0) {
System.out.print("Error: Division by zero is not allowed.");
return 0;
}
return a / b;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first number:");
int a = sc.nextInt();
System.out.println("Enter the second number:");
int b = sc.nextInt();
int result = divide(a, b);
if (b != 0) {
System.out.println("The result is: " + result);
}
sc.close();
}
}