CSIT139 C++ Write a program determines if a number represents a leap year or not
ID: 3873906 • Letter: C
Question
CSIT139 C++
Write a program determines if a number represents a leap year or not. If the number is less than 1 then the program displays the phrase "not leap year". If 400 divides the number without remainder the program displays the phrase "leap year". If 4 divides the number without remainder and 100 divides the number with remainder then the program displays the phrase "leap year". If these conditions are false then the program displays the phrase "not leap year". If possible write only 1 multi-way if statement with compound conditions to solve the problem.
Test your if statement or if statements with these numbers: -1492, 1776, 1865, 1900, 2000, and 2014
-1492 is not a leap year.
1776 is a leap year
1865 is not a leap year.
1900 is not a leap year
2000 is a leap year
2014 is not a leap year
Explanation / Answer
#include<iostream>
using namespace std;
// function that takes year as parameter
void isLeap(int year)
{
// year is less than 1
if(year<1)
cout << year << " is not leap year"<<endl;
// 400 divides the number without remainder
else if(year%400 == 0)
cout << year << " is leap year"<<endl;
//4 divides the number without remainder and 100 divides the number with remainder
else if(year%4==0 && year%100!=0)
cout << year << " is leap year"<<endl;
// none of the above conditions are met
else
cout << year << " is not leap year"<<endl;
}
main()
{
// uncomment below 2 lines if you want to take user input
// int year;
//cout << "Enter year: ";
//cin >> year;
// calling function to check if they are leap years
isLeap(-1492);
isLeap(1776);
isLeap(1865);
isLeap(1900);
isLeap(2000);
isLeap(2014);
}
SAMPLE OUTPUT
#include<iostream>
using namespace std;
// function that takes year as parameter
void isLeap(int year)
{
// year is less than 1
if(year<1)
cout << year << " is not leap year"<<endl;
// 400 divides the number without remainder
else if(year%400 == 0)
cout << year << " is leap year"<<endl;
//4 divides the number without remainder and 100 divides the number with remainder
else if(year%4==0 && year%100!=0)
cout << year << " is leap year"<<endl;
// none of the above conditions are met
else
cout << year << " is not leap year"<<endl;
}
main()
{
// uncomment below 2 lines if you want to take user input
// int year;
//cout << "Enter year: ";
//cin >> year;
// calling function to check if they are leap years
isLeap(-1492);
isLeap(1776);
isLeap(1865);
isLeap(1900);
isLeap(2000);
isLeap(2014);
}
SAMPLE OUTPUT
-1492 is not leap year 1776 is leap year 1865 is not leap year 1900 is not leap year 2000 is leap year 2014 is not leap year