Java program to add two numbers. Addition in java. Using scanner class.

 Java program to add two numbers. Addition in java using scanner class.


In this tutorial we are going to discuss about how to add two integer value in java

or a simple addition program in java. Using scanner class. Or from user input. 



Define:-First we have to declare class

Thereafter make two integer type of variable (num1 and num2 )and add this to another integer type variable name (result)

As shown in below code. 



Approach :-1

 import java.util.Scanner;
 class Addition{
 	public static void main(String[] args) {
 		Scanner s1=new Scanner(System.in);
 		System.out.println("Enter the 1st number");
 		int num1=s1.nextInt();
 		System.out.println("Enter the 2nd number");
 		int num2=s1.nextInt();

 		int total=num1+num2;

 		System.out.println("The addition of two number is"+total);
 	}
 } 

Output :-


Define:-It is the second approach to add two numbers without using (String []args) 

Let's see how this could happen. 

In java there is method known as var-arg method. Using this method we add two no. 

Let's see the code

Approach:-2

import java.util.Scanner;
 class Addition{
 	public static void main(String...java) {
 		Scanner s1=new Scanner(System.in);
 		System.out.println("Enter the 1st number");
 		int num1=s1.nextInt();
 		System.out.println("Enter the 2nd number");
 		int num2=s1.nextInt();

 		int total=num1+num2;

 		System.out.println("The addition of two number is"+total);
 	}
 }


Output:-




#Tag

Addition in java. 

Addition Java using scanner class. 

Java addition program from user input.

Addition program in java.

Java addition program. 



0 K comment

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel