Java Varargs - Simple Addition Hackerrank Solution
For Explanation Watch Video:
Sample Input
1
2
3
4
5
6
Sample Output
1+2=3
1+2+3=6
1+2+3+4+5=15
1+2+3+4+5+6=21
Code:
import java.io.*;import java.lang.reflect.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;
class Add{ public static void add(int... arr){ int sum = 0; for(int i=0;i<arr.length-1;i++){ System.out.print(arr[i]+"+"); sum += arr[i]; } sum += arr[arr.length-1]; System.out.print(arr[arr.length-1]+"="); 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(); } }
}
Comments
Post a Comment