Addition of Numbers in Java. Java Programming

Addition of Numbers in Java




In Java, finding the sum of two or more numbers is very easy. First, declare and initialize two variables to be added. Another variable to store the sum of numbers. Apply mathematical operator (+) between the declared variable and store the result. The following program calculates and prints the sum of two numbers.

1).Simple addition Program Just Using Variables:
 public class sumofnumbers{  
   public static void main(String[] args) {  
     int a=55;  
     int b=20;  
     int c=a+b;  
     System.out.println("Addition of Two Number is:"+c);  
   }  
 }  

Addition of Numbers by Taking User Input in Java 

2).Addition of Numbers Using Scanner class:

The Java Scanner class allows us to read input from the user. We take two numbers as input and pass them to the user-defined method sum(). The following program calculates the sum of two numbers using the method and prints the result.
 import java.util.Scanner;  
 public class sumofnumbers {  
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 c=a+b; System.out.println("Addition of Two Number is:"+c); } }


Post a Comment

0 Comments