Java Varargs - Simple Addition

Sort by

recency

|

297 Discussions

|

  • + 0 comments

    class Add{ public void add(int n1,int n2){ System.out.println(n1+ "+" +n2+ "=" +(n1+n2)); } public void add(int n1,int n2,int n3){ System.out.println(n1+ "+" +n2+ "+" +n3+ "=" +(n1+n2+n3)); } public void add(int n1,int n2,int n3,int n4){ System.out.println(n1+ "+" +n2+ "+" +n3+ "+" +n4+ "=" +(n1+n2+n3+n4)); } public void add(int n1,int n2,int n3,int n4,int n5){ System.out.println(n1+ "+" +n2+ "+" +n3+ "+" +n4+ "+" +n5+ "=" +(n1+n2+n3+n4+n5)); } public void add(int n1,int n2,int n3,int n4,int n5,int n6){ System.out.println(n1+ "+" +n2+"+" +n3+ "+" +n4+ "+" +n5+"+"+n6+ "=" +(n1+n2+n3+n4+n5+n6)); } }

  • + 0 comments
    import java.io.*;
    import java.lang.reflect.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    class Add
    {
        void add(int...nums){
            int sum=0;
            for(int i=0;i<nums.length;i++)
            {
                sum+=nums[i];
                if(i>0)
                {
                   System.out.print("+");
                }
                System.out.print(nums[i]);
            }
            System.out.println("="+sum);
        }
    }
    
    
    public class Solution {
    
        public static void main(String[] args) {
           try{
    			BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    			int n1=Integer.parseInt(br.readLine());
    			int n2=Integer.parseInt(br.readLine());
    			int n3=Integer.parseInt(br.readLine());
    			int n4=Integer.parseInt(br.readLine());
    			int n5=Integer.parseInt(br.readLine());
    			int n6=Integer.parseInt(br.readLine());
    			Add ob=new Add();
    			ob.add(n1,n2);
    			ob.add(n1,n2,n3);
    			ob.add(n1,n2,n3,n4,n5);	
    			ob.add(n1,n2,n3,n4,n5,n6);
    			Method[] methods=Add.class.getDeclaredMethods();
    			Set<String> set=new HashSet<>();
    			boolean overload=false;
    			for(int i=0;i<methods.length;i++)
    			{
    				if(set.contains(methods[i].getName()))
    				{
    					overload=true;
    					break;
    				}
    				set.add(methods[i].getName());
    				
    			}
    			if(overload)
    			{
    				throw new Exception("Overloading not allowed");
    			}
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    		
    	
    	
    
    }
    
  • + 0 comments

    class Add { void add(int... number) {

        int counter = 0;
        int sumNum = 0;
    
        for (int i = 0; i < number.length; i++) {
    
            sumNum = sumNum + number[i];
            counter++;
            if(counter<number.length){
                System.out.printf("%d+", number[i]);
            }else{
                System.out.printf("%d", number[i]);
            }
    
        }
        System.out.printf("=%d%n", sumNum);
    
    }
    

    }

  • + 0 comments

    class Add { public void add(int... x) { //int r = 0; String seperator = ""; int count = 0; int result = 0; for(int a : x) { //System.out.println(count+a); count = count + a; //System.out.println(a); System.out.print(seperator +a); seperator = "+";

       }
       result = count;
       System.out.print("="+result);
       System.out.println();
    

    } }

  • + 0 comments

    class Add{
    public void add(int... a){ int sum = 0; int plus = a.length-1; for(int x:a){ sum += x; System.out.print(x);
    if(plus>0){ System.out.print("+"); plus--; }
    } System.out.print("="+sum); System.out.println(); } }