Question

Test if a year is a leap year. Do this by creating a function named is...

Test if a year is a leap year. Do this by creating a function named is LeapYear. This function will not return anything it will just print (“\(year) is a leap year”) or (“\(year) is not a leap year”) (using language swift)?

a. The function should accomplish the following

b. Get user input to define a year

c. Divide the input by 4. If there is a remainder it is not a leap year.

d. Divide the input by 100. If there is no remainder it is a leap year

e. Divide the input by 400. There is a remainder the year is not a leap year. If there is no remainder it is a leap year

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//LeapYear __FUNCTION__
func LeapYear(year: Int) -> Void {
    if year % 4 != 0 { //Divide the input by 4. If there is a remainder it is not a leap year.
        print("\(year) is not a leap year")
    } else { 
    /* Divide the input by 100. If there is no remainder it is a leap year
       Divide the input by 400. There is a remainder the year is not a leap year. 
       If there is no remainder it is a leap year */
       
        if year % 100 == 0 && year % 400 != 0 { 
            print("\(year) is not a leap year")
        } else {
            print("\(year) is a leap year")
        }
    }
}

//Taking user input
print("Enter year: ")
if let input = readLine() //user input as String
{
    if let int = Int(input) //converting user input to type Integer
    {
        LeapYear(year: int) //Calling the LeapYear __FUNCTION__
    }
    else{
        print("\(input) of not a valid Integer") //if user input is not a valid Integer
    }
}

OUTPUT

Enter year: 2014 2014 is not a leap year .. Program finished with exit code o Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
Test if a year is a leap year. Do this by creating a function named is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary...

    FINDING THE LEAP YEARS (14 points) From Wikipedia, "A leap year (also known as an intercalary year or bissextile year) is a calendar year containing one additional day (or, in the case of lunisolar calendars, a month) added to keep the calendar year synchronized with the astronomical or seasonal year." In the Gregorian calendar, which is in use today, to determine leap year: A year is a leap year if it is divisible by 400 Or it is divisible by...

  • I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you!

    I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you! Write a program that asks the user to enter a date (e.g. July 4, 2008) and will return the day of the week that corresponds to that date. Your program should take the input in numeric form, thus the input prompt should be as follows: Please enter a date below: Enter month (1-12): Enter day (1-31) Enter year...

  • Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following...

    Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following math function with x the input argument and y the output y(x)=0.8x4-13x2-5x The function should work for x being a scalar or a vector. Write a script file named PBTask4pl.m to a) Use the function to calculate y(3) and y(5) and display the results in command window b) Use the function to make a plot of the function y(x) for -5:5: x 5:5. Label...

  • Write a program that accepts a year and determines whether the year is a leap year....

    Write a program that accepts a year and determines whether the year is a leap year. Use the mod function. The output should be the variable extra_day, which should be 1 if the year is a leap year and 0 otherwise. The rules for determining leap years in the Gregorian calendar are as follows: All years evenly divisible by 400 are leap years. Years evenly divisible by 100 but not by 400 are not leap years. Years divisible by 4...

  • CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap...

    CIS22A Homework 6 Topic: Chapter 4 & 5 Purpose: Determine if a year is a leap year Create a new C++ project titled “HW 6” in the CodeBlocks IDE. Use the “Console Application” project option. Over time, the calendar we use has changed a lot. Currently, we use a system called the Gregorian Calendar and it was introduced in 1582. The algorithm to determine if a year is a leap year is as follows: Every year that is exactly divisible...

  • Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the...

    Java Programming USE ONLY 1 CLASS. Class main is all that is required 1. Ask the user for a year as an input (positive integer - should be between 1500 and 2020, and can include 1500 and 2020). 2. If the year input is not between 1500 and 2020, do not check for leap year. Instead, print that this year cannot be checked. 3. If the input year provided by the user is a leap year, print that year is...

  • Question is two parts In R(Programming language). Define and write a function which takes one number...

    Question is two parts In R(Programming language). Define and write a function which takes one number as input and detects whether the number is even. Return TRUE if it is even, and FALSE otherwise. You need to first detect if the input is an integer; if not, return NA. Note 1: TRUE and FALSE are logical values, not strings! Note 2: No need to get user input from the console. Test your function using your_function_name(1), your_function_name(2), and your_function_name(1.5). Hint: What...

  • In the space below, write a C function definition for a function named StrLower, which will...

    In the space below, write a C function definition for a function named StrLower, which will receive one parameter, a pointer to a null-terminated string (i.e., pointer to char), convert each character in the string to lowercase (using the tolower function from <ctype.h>), and return nothing to the caller. Inside the function definition, you may use a for loop or a while loop. If you use any variables other than the incoming parameter, you must define them locally in the...

  • Write a program that will do the following. The main function should ask the user how...

    Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...

  • Using Swift, Write a program that reads in a string and passes to a function named...

    Using Swift, Write a program that reads in a string and passes to a function named parse(digit:) that takes a string with one character as parameter. The function should return -1 if the input is not a digit character and the digit otherwise. You must use a switch statement parse(digit: "1") // 1 parse(digit: "3") // 3 parse(digit: "a") // -1

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT