-
-
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) |
I tried this too.
//Don't change this
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
var leap = "NO"
//IF divisible by 4 with no remainders.
if year.isMultiple(of: 4) {
leap = "YES"
//Is leap year, unless:
}
if year.isMultiple(of: 100) {
leap = "NO"
//Is not leap year, unless:
}
if year.isMultiple(of: 400) {
leap = "YES"
//Is leap year.
}
print(leap)
}
//Don't change this
isLeap(year: aYear)
func aLeap (year: Int)
{
if year%4 == 0
{
if year%100 == 0
{
if year%400 == 0
{
print ("\(year) is a leap year")} //print("yes")
else
{
print ("\(year) is not a leap year")} //print("no")
}
}
}
if year % 4 == 0 && year % 100 != 0 {
print("YES")
} else if year % 4 == 0 && year % 100 == 0 {
if year % 400 == 0 {
print("YES")
} else {
print("NO")
}
}
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
var leap = "No"
if aYear % 4 == 0 {
leap = "Yes"
if aYear % 100 != 0 {
leap = "Yes"
}else if aYear % 400 == 0 {
leap = "Yes"
}else{
leap = "No"
}
}
print (leap)
}
isLeap(year: aYear)
This worked for me:
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
//Write your code inside this function.
if (year%4 == 0 && year%100 != 0 || year%400 == 0) {
print("YES")
} else {
print("NO")
}
}
why when I wrote the code below then just had two answers?
//Don't change this var aYear = Int(readLine()!)! func isLeap(year: Int) { //IF divisible by 4 with no remainders. if year % 4 == 0 { print("YES") } if year % 100 == 0 { print("NO") } if year % 400 == 0 { print("YES") } } //Don't change this isLeap(year: aYear)
and the result just showed:
YES
NOwhat's the different between using var() and print() in this challenge?
The diffrence between using var() and print is that every time you assign value to the variable leap it changes itself value so when you print the leap variable value it only shows the latest value he had
any reason why I would get this: Expressions Are Not Allowed At The Top Level
Maybe a little more complicated than need be, but the easiest way for me to include the occasion 1300 and 1200.
func isLeap(year: Int) {
//divisible by four
var leap = String()
if year % 4 == 0 && year % 100 != 0{
leap = "YES"
}
else if year % 4 == 0 && year % 100 == 0 && year % 400 != 0 {
leap = "NO"
}
else if year % 4 == 0 && year % 100 == 0 && year % 400 == 0 {
leap = "YES"
}
print(leap)
}
Here is my solution:
func isLeap(year: Int) {
if year % 400 == 0 {
print("YES")
} else if year % 4 == 0 && year % 100 != 0 {
print("YES")
} else {
print("NO")
}
}
isLeap(year: 2000) // write here whatever number you want
My turn! Here is my solution :
`
//Don't change this
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
// if year is divisible by 4 AND 100 and 400 with no remainder so it must be a leap year
if year % 4 == 0 && year % 100 == 0 && year % 400 == 0 {
print("YES")
// If the three precedent conditions are not met : if year is divisible by 4 AND 100 with no remainder but not by 400 it is not a leap year
} else if year % 4 == 0 && year % 100 == 0 && year % 400 != 0 {
print("NO")
// else, if the precedent conditions are not met and year is only divisible by 4 with no remainder, it should be a leap year
} else if year % 4 == 0 {
print("YES")
// In all other cases, it should not be a leap year
} else {
print("NO")
}
}
`
Hope it helps and sorry for my bad English
//below you can change the year to whatever you'd like to test
var aYear = 1201
func isLeap(year: Int) {
if aYear%4 == 0 && aYear%100 != 0 {
//above checks to see if the year is evenly divisible by 4 and not evenly divisible by 100
// as long a the year is DIVISIBLE BY 4 (no remainder) and NOT DIVISIBLE BY 100 (needs a remainder) ITS A LEAP YEAR
print("The year \(aYear) is a leap year.")
} else if aYear%4 == 0 && aYear%100 == 0 && aYear%400 == 0 {
//above checks to see if the year is evenly divisible by 4, 100, and 400
//if the year IS DIVISIBLE BY 4 AND 100 (no remainder) then for it to be a leap year it would ALSO HAVE TO BE DIVISIBLE BY 400 (no remainder)
print("The year \(aYear) is a leap year.")
} else {
//any other year that doesn't meet the above conditions would not be considered a leap year
print("The year \(aYear) is not a leap year.")
}
}
isLeap(year: aYear)
import UIKit
var aYear = 1200
if aYear % 4 == 0 && aYear % 100 == 0 && aYear % 400 == 0 {
print("YES")
} else if aYear % 4 == 0 && aYear % 100 != 0 {
print("YES")
} else {
print("NO")
}
//Don't change this var aYear = Int(readLine()!)!
func isLeap(year: Int) {
if year / 4 == 0 || year % 100 != 00 || year % 400 == 0 { print("YES") } else { print("NO") }
}
//Don't change this isLeap(year: aYear)
THIS WORKED
After trying the prior solutions, I found this simple and to the point solution worked. Thanks @surkeela for this. You rock!
what worked for me is:
{ if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 {
print("YES")
} else {
print("NO")
} }
This is what worked for me on Playground:
var aYear = 1200 //type in year here.
func isLeap(year: Int) {
//Write your code inside this function.
if aYear % 4 == 0 && aYear % 100 != 0 || aYear % 400 == 0 {print("YES")}
else{print("NO")}
}
isLeap(year: aYear)
I've made a different code and it works in Replit.
But when I put it in exercise swift, which is located inside the lesson, it doesn't approved as a solution.
Why? In Replit it works absolutely correctly..
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
let a = aYear % 4
let b = aYear % 100
let c = aYear % 400
if a == 0 && a != b {print ("YES")}
else if a == c {print ("YES")}
else {print ("NO")}
}
isLeap(year: aYear)
//Don't change this
var aYear = 2020
// (readLine()!)! -readLine() is used to read the input from the user. <-------------------
func isLeap(year: Int) {
if year%4 == 0 && year%100 != 00 || year%400 == 0{
print("Yes")
} else{
print("No")
}
}
//Try out your function with some different years. Don't copy the line below (it's not part of the exercise you need to complete).
isLeap(year: 2020)
//Don't change this
var year = 2000
func isLeap(aYear: Int) {
if (aYear % 4 == 0 || aYear % 400 == 0){
print("YES")
}else if (aYear % 100 == 0){
print("NO")
}else {
print("NO")
}
//Write your code inside this function.
}
//Try out your function with some different years. Don't copy the line below (it's not part of the exercise you need to complete).
isLeap(aYear: year)
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)
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
//Don't change this
var aYear = Int(readLine()!)!
func isLeap(year: Int) {
//Write your code inside this function.
if year % 100 == 0 && year % 400 == 0 {
print("YES")
}else if year % 100 == 0 {
print("NO")
} else if year % 4 == 0 {
print("YES")
} else {
print("NO")
}
}