Ankaj Gupta
August 09, 2020

Division of two numbers in java numbers

Java program for Division two Integers

In this section, you will learn how to divide two numbers in Java. Let's see :

1.) First program :

In this program, we specify the value of both the numbers in the program itself.

Let's try to create a simple example :

  ➤ Example : divide two integers with predefined value;

class DivideTwoNumbers 
{
   public static void main(String args[]) {      
      int x = 15, y = 10, divide;
      divide = x / y;
      
      System.out.println("Division of two numbers is : "+divide);
   }
}
                            
Explanation :

 Example 1 : explanation

    In the above program, we have created three integer variables x, y and divide, then stored (15 and 10) values in x and y.

  • Then, x divide by y using the / operator, and its result is stored in another variable divide.

  •  

  • Finally, divide is printed on the screen using System.out.println() function.

2.) Second program :

In this program, we will take two inputs from the user(entered by the user) and prints the division.

Note : Scanner class is a part of package(java.util), so we required to import this package in our Java program.

Let's try to create a simple example :

  ➤ Example : Division two integers with user input(Scanner);

import java.util.Scanner;
class DevideTwoNumbers 
{
   public static void main(String args[]) {      
      int x, y, devide;

      System.out.println("Enter two integers to calculate their Division : ");
      Scanner in = new Scanner(System.in);
     
      x = in.nextInt();
      y = in.nextInt();
      divide = x / y;
     
     System.out.println("Division of two numbers is : "+divide);
   }
}
                            
Explanation :

 Example 2 : explanation

    In the above program, we have created three integer variables x, y and divide, then create a Scanner class so that you can take input from the user for x and y.

  • Then, x divide by y using the / operator, and its result is stored in another variable divide.

  •  

  • Finally, divide is printed on the screen using System.out.println() function.

Java program Java programming

Join the discussion

Comments

0 comments

No comments yet — be the first to share your thoughts.

Related Posts