See the Following Program
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.
Help with Code Tags
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;
-
}
-
}
--------------------------------------------------------------------------------------------------------------------------------------------------
The One More Sample Of the Program
C program to check whether a year is a leap year or not.
Code: C
/*
** C program to check whether an entered year is a leap year or not
** @author: Pradeep
** @date: 02/12/06
*/#include<stdio.h>int main
(void){ int year;
printf("Enter the year: ");
scanf
("%d",&year
);
/*
** The logic is that the year is either divisible by both
** 100 and 4 , OR its only divisible by 4 not by hundred
*/ if(year%
400 ==
0 ||
(year%
100 !=
0 && year%
4 ==
0)) { printf("Year %d is a leap year",year
);
} else { printf("Year %d is not a leap year",year
);
} return 0;
}
If Not Satisfied Just put the Program name in Google and Search
×