Java Program To Subtract Two Numbers : In different ways

Java program to subtract two numbers or two integers in different ways .These different ways are  without user input, user input at the command line, input at run time .

Java Program To Subtract Two Numbers

Subtraction of two number without user input:

Already assign a value of two number at the time of programming . User compile and run the program then show the sum of two number in the output screen.

 Describe the following program:

Create a class , write main method ,under the main method declare variable with data type, assign value on variable, subtract two num1 and num2 and print the result .User first open command prompt compile and after run this program.
class Subtraction
 {
  public static void main(String args[])
   {
    int num1=15,num2=10,subtract;
    subtract=num1-num2;
    System.out.print("Subtraction of two numbers = "+subtract);  
   }
 }

Output:

Subtraction of two numbers = 5

Subtraction of two number user input at the command line:

Enter the value of two number at the command line then show the subtract of these two number in the output screen.
class Subtraction
 {
  public static void main(String args[])
   {
    int num1,num2;
     num1 = Integer.parseInt(args[0]);  
     num2 = Integer.parseInt(args[1]);  
     System.out.println("Subtraction of two numbers = "+(num1-num2));
   }
 }

Output:

Subtraction of two numbers = 40

Subtraction of two number with user input at run time:

Enter the value of two number at the run time then show the subtract of these two number in the output screen.
import java.util.Scanner;
class Subtraction
 {
 public static void main(String args[])
  {    
   Scanner sc = new Scanner(System.in);
   int num1,num2,subtract;
    System.out.print("Enter a first no : ");
    num1 = sc.nextInt();
    System.out.print("Enter a second no : ");
    num2 = sc.nextInt();
    subtract=num1-num2;
    System.out.print("Subtraction of two numbers are : "+subtract);
   }
 }

Output:

Enter a first no : 30
Enter a second no : 10
Subtraction of two numbers are : 20

More related java program posts:

Post a Comment

0 Comments