Latest News: Addition of Two Numbers Program in Java by Taking Input From User

Addition of Two Numbers Program in Java by Taking Input From User

 Add two Numbers Program on Java



Example1: Sum of two numbers

In the example, we specify the value in the program itself.

public class AddTwoNumbers 
public static void main(String[] args) 
int n1 = 5, n2 = 15, s; 
s = n1 + n2; 
System.out.println("Sum of these numbers: "+s); 
}


Example2: Sum of two numbers with Scanner Library

The scanner allows user input so we can get the values from the user. 
import java.util.Scanner; 
public class AddTwoNumbers2 

public static void main(String[] args) 

int n1, n2, s; 

Scanner sc = new Scanner(System.in); 
System.out.println("Enter First Number: "); 

n1 = sc.nextInt(); 
System.out.println("Enter Second Number: "); 

n2 = sc.nextInt(); 
sc.close(); 

s = n1 + n2; 
System.out.println("Sum of these numbers: "+s);

}






Popular Posts

MyMarket