Problem:
Given 3 int values, a b c, return their sum. However, if one of the values is 13 then it does not count towards the sum and values to its right do not count. So for example, if b is 13, then both b and c do not count.
luckySum(1, 2, 3) → 6
luckySum(1, 2, 13) → 3
luckySum(1, 13, 3) → 1
Solution:
public int luckySum(int a, int b, int c) { if (a == 13) { a = 0; b =0; c =0; } if (b == 13) { b=0; c=0; } if (c == 13) { c=0; } return a + b + c; }
public int luckySum(int a, int b, int c) {
ReplyDeleteif(a==13 && b==13 && c==13){
return 0;
}else if(a==13){
return 0;
}else if(c==13 && b!=13){
return a+b;
}else if(b==13){
return a;
}else{
return a+b+c;
}
}
return(a==13)? 0: (b==13)? a:(c==13)? a+b:a+b+c;
ReplyDeletebravo
DeleteCan anyone explain me as I'm unable to understand it
ReplyDeletepublic int luckySum(int... nums) {
ReplyDeleteint sum=0;
for(int n:nums){
if(n==13)
return sum;
else
sum+=n;
}
return sum;
}