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) return 0; else if (b == 13) return a; else if (c == 13) return a + b; else return a + b +c; }
public int luckySum(int a, int b, int c) {
ReplyDeleteint sum = 0;
if (a != 13) sum += a;
else return sum;
if (b != 13) sum += b;
else return sum;
if (c != 13) sum += c;
return sum;
}
int sum = 0;
ReplyDeleteif (a != 13 && b !=13 && c != 13) {return sum = sum + a+b+c;}
else if (a==13 && b!=13 && c!=13){return sum = sum; }
else if (b == 13 && c != 13 && a!=13) {return sum = sum +a;}
else if (c==13 && a!=13 && b!=13) {return sum = sum +a+b;}
else if (b==13 && b==c) {return sum = sum +a;}
else {return sum;}
public int luckySum(int a, int b, int c) {
ReplyDeleteint[] arr = {a,b,c};
int sum = 0;
for (int i = 0; i< arr.length; i++){
if(arr[i] == 13){
break;
}
sum += arr[i];
}
return sum;
}
public int loneSum(int a, int b, int c) {
ReplyDeleteif (a == b && a == c && b == a){
return 0;
} else if(b == c){
return a;
} else if(a == c){
return b;
} else if(a == b) {
return c;
} else {
return a + b + c;
}
}
public int luckySum(int a, int b, int c) {
ReplyDeleteif( a != 13 && b != 13 && c != 13){
return a + b + c;
} else if( a == 13){
return 0;
} else if (b == 13){
return a;
} else if (c == 13){
return a + b;
} else {
return 0;
}
}
return (a==13)?0:(b==13)?a:(c==13)?a+b:a+b+c;
ReplyDeleteprivate final int luckySum(int a, int b, int c) {
ReplyDeleteif (a == 13){
return 0;
}
if (b == 13){
return a;
}
if (c == 13){
return a + b;
}
return a + b + c;
}
public int luckySum(int a, int b, int c)
ReplyDelete{
int sum = 0;
if (a != 13)
{
sum = a + b;
if(b == 13)
{
sum -= b;
}
if(b != 13 && c!= 13)
{
sum += c;
}
}
return sum;
}