Swift 4.0.3 /*Write a program that prompts a user for the current month and year
ID: 3879442 • Letter: S
Question
Swift 4.0.3
/*Write a program that prompts a user for the current month and year and for a month and year in the past. The program should output how far in the past the past date is. The input and output should be something like:
Enter Past Month: 12
Enter Past Year: 2013
Enter Current Month: 08
Enter Current Year: 2015
12-2013 was 1 year(s), 8 month(s) ago.
You must use safe string to integer conversion for this exercise. Your program should not crash under any circumstances (no matter how silly the user’s input). Nor should the word “Optional” appear anywhere in your code’s output. */
Explanation / Answer
Here is program for your requirement.It ask from user to enter past month and year.Then it will ask for current month and year.And will display the output.
print("Enter Past Month:");
let pastmonth = readLine();
var Intpastmonth=Int(pastmonth)
print("Enter Past Year:");
let pastyear = readLine();
var Intpastyear=Int(pastyear)
print("Enter Current Month:");
let currentmonth = readLine();
var Intcurrentmonth = Int(currentmonth)
print("Enter Current Year:");
let currentyear = readLine();
var Intcurrentyear = Int(currentyear)
var Monthdiff=abs(Intcurrentmonth-Intpastmonth)
var Yeardiff=abs(Intcurrentyear-Intpastyear)
print("(Intpastmonth)-(pastyear) was (Yeardiff) years, (Monthdiff) months ago.")
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.