944,087 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 863
  • C RSS
Oct 16th, 2009
0

how can i make a program that inputs month and year, and then output the days of the

Expand Post »
how can I make a program that inputs the month and year. and then should output the days of the month.
-january, march, may, july, august, october, and dicember have 31 days.
-april, june, september, and november have 30 days.
-february has 28 days in non-leap years and 29 in leap years.

please I need the code...
Reputation Points: 5
Solved Threads: 0
Newbie Poster
needhelpe is offline Offline
22 posts
since Oct 2009
Oct 16th, 2009
-7
Re: how can i make a program that inputs month and year, and then output the days of the
Take it in small steps. First write a program that just lets you enter the year. Get that working then add more that lets you enter the month and day of month. If you enter the month as a number you can then create a switch statement that calculates the number of days in the month, as you have already posted.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Oct 16th, 2009
0
Re: how can i make a program that inputs month and year, and then output the days of the
I doubt you'll get any code for free here. So here's an outline of how you might go about it.
Grab the input from the user and compare strcmp() it against references, i.e. "January", etc (might want to consider how you'd work with upper and lower case letters).
Then grab the year and see if it's a leap year (only necessary if the month is Feb though.

Have a go at trying this and get back to us with problems.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 16th, 2009
0
Re: how can i make a program that inputs month and year, and then output the days of the
start the main function

declare two int variables

int year, month;
read the year and month values
just a scanf statement with to arguments .

now starts the procedure:

first check whether given year is a leap year or not
a leap year comes for every four years.(so it is divisible by four)

a century year is a leap year if its divisible by 400.
that means any year divisible only by 100 is not a leap year, if not divisible by 400.

so club all these conditions in if ( year % 4 ==0 || !( year %100==0)&&year %400 ==0)

so the year is a leap year
take a two dimentional array
int years[2][12];

and assign the value starting form january,
years[0][0] =31, years[0][1]=28 (non leap) till years[0][11]=31 december
january , years[1][0] =31, years[1][1]=29 (leap) till years[1][11]=31 december

from the pogram find out what is the month

so chose the appropriate index for the year and month

you should get it with above hint!
Reputation Points: 13
Solved Threads: 3
Junior Poster
Gaiety is offline Offline
115 posts
since Sep 2009
Oct 18th, 2009
0
Re: how can i make a program that inputs month and year, and then output the days of the
ive been learning this on my math lesson, so if you want i can give you the math algorythm for this problem, and then you should be able to code that without any problem
Reputation Points: 10
Solved Threads: 0
Light Poster
OSiRiSsk is offline Offline
28 posts
since Oct 2009
Oct 18th, 2009
-7
Re: how can i make a program that inputs month and year, and then output the days of the
Click to Expand / Collapse  Quote originally posted by OSiRiSsk ...
ive been learning this on my math lesson, so if you want i can give you the math algorythm for this problem, and then you should be able to code that without any problem
There is no math involved.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Oct 18th, 2009
0
Re: how can i make a program that inputs month and year, and then output the days of the
There is no math involved.
you can look at this problem from math side, as we did in my class
Reputation Points: 10
Solved Threads: 0
Light Poster
OSiRiSsk is offline Offline
28 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how to scan arrow keys from keyboard in c?
Next Thread in C Forum Timeline: binary search





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC