-
-
Save TheMuellenator/5343c658efdd46dc04522174fa4f0cd1 to your computer and use it in GitHub Desktop.
//Don't change this | |
var aYear = Int(readLine()!)! | |
func isLeap(year: Int) { | |
var leap = "NO" | |
//IF divisible by 4 with no remainders. | |
if year % 4 == 0 { | |
leap = "YES" | |
//Is leap year, unless: | |
} | |
if year % 100 == 0 { | |
leap = "NO" | |
//Is not leap year, unless: | |
} | |
if year % 400 == 0 { | |
leap = "YES" | |
//Is leap year. | |
} | |
print(leap) | |
} | |
//Don't change this | |
isLeap(year: aYear) |
Tried to shorten the code lines. But this did not work on playground.
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
if year % 4 == 0 && year % 400 == 0 && year % 100 != 0 {
print("Yes")
} else {
print("No")
}
}
isLeap(year: aYear)
Here's the buildtime message:
error: Playground execution aborted: error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18f6dd21c).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
What could be wrong?
Who can explain what this line is for? Xcode gives an error on it
//Don't change this var aYear = Int(readLine()!)!
This is the request for an input, in this case the year to check for a leap year
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 {
print("\(year) is a leap year.")
} else {
print("\(year) is not a leap year.")
}
}
isLeap(year: aYear)
it is not working for me.
What is the issue you are getting?
could someone please help me out here, i understand the steps necessary to get isLeap to say whether or not the year is a leap year but i cannot for the life of me understand the "==0" expression.
- if year % 4 == 0 print yes, 2000 divided by 4 is 500 which is a whole number with no remainder but is not equal to zero
- if year % 100 != 0 print no, 2000 divided by 100 is equal to 20 which again is not equal to zero but again is a whole number and finally 3. if year % 400 == 0 print yes 2000 divided by 400 is 5 which is a whole number but not equal to zero.
basically i wanna know what zero represents, is it an expression for a whole number?
So here the "==0" is calculate based on the modulus (remainder). Using the example 2000%4 which means that how many multiples of 4 will fit inside the value 2000 and returns the value that's left over which is 0 in this case.
More details check the screenshot below:
In the screenshot you can see at Line89 that the remainder of 2000%4 is 0 as there is no remainder.
If there is anything feel free to ask.
Thanks.
func isLeap(year: Int) {
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
print("YES")
}
else{
print("NO")
}
}
var aYear = 2020
func isLeap(year: Int) {
if year % 4 == 0 && year % 100 != 0 {
print("Yes, Leap Year")
} else if year % 100 == 0 && year % 400 == 0 {
print("Yes, A leap year")
} else {
print("No, Not a leap year")
}
}
isLeap(year: aYear)
Check my answer
var aYear = 1904
func isLeap(year: Int) {
if aYear % 4 == 0 {
if aYear % 100 == 0 && aYear % 400 != 0 {
print("No")
} else {
print("Yes")
}
} else {
print("No")
}
}
isLeap(year: aYear)