| | |
leap year
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
Leap Years
Def: A year is a century year if it is divisible by 100.
Def: A year is a non-century year if it is not a century year.
Def: A year is a leap year if it is a non-century year that is divisible by 4, or a century year that is divisible by 400. Nothing else is a leap year.
In a source file named leapyears.cpp, write a program that will prompt the user for the starting year and ending year for a range of years and print to the screen all leap years in that range, 5 years per line. You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. Your main function will call the isLeapYear function in a loop for every year within the range delineated by, and including, the starting and ending years.
that's what i have, any ideas how to fix it?
thanks!
Def: A year is a century year if it is divisible by 100.
Def: A year is a non-century year if it is not a century year.
Def: A year is a leap year if it is a non-century year that is divisible by 4, or a century year that is divisible by 400. Nothing else is a leap year.
In a source file named leapyears.cpp, write a program that will prompt the user for the starting year and ending year for a range of years and print to the screen all leap years in that range, 5 years per line. You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. Your main function will call the isLeapYear function in a loop for every year within the range delineated by, and including, the starting and ending years.
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool LeapYear(int y) { return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0); } double isLeapYear(double x) { if( x > -10000 && x < 10000) return x; } int main() { int x; cout << "Please enter start and end of a range of years: "; cin >> x; if (isLeapYear(x)) { if (LeapYear(x)) cout << x; } }
thanks!
Last edited by WaltP; Apr 6th, 2007 at 2:45 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
>>any ideas how to fix it?
fix what? you didn't say what's wrong with the code you posted.
why does isLeapYear() take a double as parameter? It should be an integer; afterall years do not contain fractions.
fix what? you didn't say what's wrong with the code you posted.
why does isLeapYear() take a double as parameter? It should be an integer; afterall years do not contain fractions.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
read the assignment again, and re-read it again and again until you understand it. IsLeapYear() is supposed to return TRUE if the year is a leap year or FALSE is the year is not a leap year. The function LeapYear() that you posted seems to do that. The functon IsLeapYear() that you posted does not do that, actually I have no clue what its purposes is. Delete IsLeapYear() and rename LeapYear() function to IsLeapYear().
In function main() you have to enter two years -- your program only gets one year. Then also in main() you need to create a loop that calls IsLeapYear() for each year between the first and second years that you enter from the keyboard.
Don't forget -- years prior to 1582 AD do not have leap years because the Gregorian calendar wasn't created until February 24, 1582.
In function main() you have to enter two years -- your program only gets one year. Then also in main() you need to create a loop that calls IsLeapYear() for each year between the first and second years that you enter from the keyboard.
Don't forget -- years prior to 1582 AD do not have leap years because the Gregorian calendar wasn't created until February 24, 1582.
Last edited by Ancient Dragon; Apr 5th, 2007 at 11:36 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2007
Posts: 11
Reputation:
Solved Threads: 0
You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. - not sure how
also not sure how to get it to print up to five leap years per line.
also not sure how to get it to print up to five leap years per line.
c Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool isLeapYear(int y) { return ( y % 4 == 0) && (y % 100 != 0) || (y % 400 == 0); } int main() { int x, z; cout << "Please enter start and end of a range of years: "; cin >> x >> z; for(int i = x; i <= z; i++) { if (isLeapYear(i)) { cout << i << endl; } } }
Last edited by WaltP; Apr 6th, 2007 at 2:47 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
The function doesn't have to return a value to be able to display what the year is.
C Syntax (Toggle Plain Text)
void leapYear(int year) { if((year % 100) == 0 ) { printf("%d is a century year\n", year); } else if(((year % 4) == 0) || ((year % 400) == 0)) { printf("%d is a leap year\n", year); } else { printf("%d is a not century\n", year); } }
>The function doesn't have to return a value to be able to display what the year is.
It does according to the assignment:
Plus, as I see it, the program was supposed to be in C++, not C.
It does according to the assignment:
•
•
•
•
You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
C Syntax (Toggle Plain Text)
int leapYear(int year) { return((!(year % 100 == 0))&&((year % 4 == 0) || (year % 400) == 0)); }
This will return if is a leap year or not.
call it inside a loop that will start with the first year and stop at the wanted one.
Inside the loop
C Syntax (Toggle Plain Text)
if( leapYear( year )) { printf("%d ", year); }
•
•
•
•
You must write a separate function called isLeapYear that takes a year as a parameter and returns whether or not that year is a leap year. - not sure how
also not sure how to get it to print up to five leap years per line.
c Syntax (Toggle Plain Text)
int counter = 0; for(int i = x; i <= z; i++) { if (isLeapYear(i)) { cout << i << " "; if( ++counter == 5) { cout << "\n"; counter = 0; } } }
Last edited by Ancient Dragon; Apr 6th, 2007 at 7:03 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
- leap year calculation (ASP)
Other Threads in the C Forum
- Previous Thread: how to distinquish a set of characters form an interger?
- Next Thread: headder files ie stdio.h
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql odf open opensource openwebfoundation owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard string suggestions systemcall test testautomation unix urboc user voidmain() wab win32api windows.h






