java program to add two numbers


Java program to add two numbers

 

public class AddNumbers{
 
     public static void main(String []args){

         int a = 10;
         int b = 20;
         int c = a + b;
         System.out.println("Sum of two numbers is  " +c);
        
     }
}

 Output: 

Sum of two numbers is   30                                                                                                                                      

Java program to find sum of 2 number using method


public class AddNumbers2{
   
    public int sum(int x, int y)
     {
         int z = x+y;
         return z;
     }

public static void main(String []args){

    AddNumbers2  an = new AddNumbers2();
         System.out.println("Sum of two numbers is: "+an.sum(15,10));
        
     }
}

Output:
 
Sum of two numbers is:  25


EmoticonEmoticon