A program that while tell you when your next even birthday is (10, 20, 30, 40, etc..)
4 views (last 30 days)
Show older comments
Andreas Larsson
on 7 Feb 2016
Commented: Star Strider
on 9 Feb 2016
So basically the program is supposed to ask for these two things:
Whats your name?
Which year are you born?
Then it will tell you the year that you turn an even number. So for instance:
My name is Andreas
I'm born 1974
Your next even birthday is:
2024
Also if you turn an even number THIS year, it will congratulate you for it
What's your name? Andreas
Which year are you born? 1996
Congratulations Andreas
You turn 20 this year.
This is how i've thought of doing so far:
name=input('Whats your name? ')
year=input('Which year are you born? ')
Then i have to write a program which calculates the next time you turn an even number.
However it can't be anything under your current age. Also on the end i'll simply just add:
disp('Your next even birthday is: ')
disp('next_even_birthday_year')
0 Comments
Accepted Answer
Star Strider
on 7 Feb 2016
Edited: Star Strider
on 7 Feb 2016
This seems to be homework, so I’ll provide part of it:
BirthYr = 1976;
ThisYr = str2num(datestr(now, 'yyyy'));
Age = ThisYr - BirthYr;
if rem(Age,10) == 0
fprintf(1,'\n\tYou turn %.0f this year.\n', Age)
else
NextYr10 = BirthYr + 10*fix(Age/10) + 10;
fprintf(1, '\n\tYour next even birthday is %.0f.\n', NextYr10);
end
I will leave it to you to make it appropriately robust and deliver the messages you want.
EDIT — I would use the inputdlg function rather than input. It’s easier and doesn’t clutter your Command Window. You will need to use str2num with its results, and the basics of cell array referencing, but neither are difficult.
2 Comments
Star Strider
on 9 Feb 2016
My pleasure, and cheers to you as well!
This was a fun problem!
The ‘1’ is not absolutely necessary any more, but it was when I learnt the essences of MATLAB in the mid 1990s, when a valid fid was required for all fprintf calls. (The fprintf function would then throw an error if a valid fid was not supplied.) The ‘1’ was then (and likely remains) standard output, (stdout). I have just always included it since, just out of habit. (I’m a rather unapologetic dinosaur in some respects.)
More Answers (0)
See Also
Categories
Find more on Birthdays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!