I need some on a c++ programming assignment where I am required to create a program that checks for a leap year using a do while loop and also allows the user to exit the program.
This is what I have done so far (not sure if I have done it right so far..)
Please help!
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a year: ";
cin >> year;
while(year!=0)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
}
return 0;
}
#include <iostream>
int main()
{
int year ;
std::cout << "Enter a year: ";
// if the user entered an year in the gregorian calendar
if( std::cin >> year && year > 1572 )
{
bool leap ;
if( year%400 == 0 ) leap = true ; // years divisible by 400 are leap years
elseif( year%100 == 0 ) leap = false ; // years divisible by 100 but not divisible by 400 are not leap years
else leap = year%4 == 0 ; // otherwise, years divisible by 4 are leap years
if(leap) std::cout << year << " is a leap year\n" ;
else std::cout << year << " is not a leap year\n" ;
// alternatively, we can combine the conditions into a single if
// if ( the year is exactly divisible by 4 and not divisible by 100 )
// or ( year is exactly divisible by 400 )
// then the year is a leap year.
if( ( ( year%4 == 0 ) && ( year%100 != 0 ) ) || ( year%400 == 0 ) )
std::cout << year << " is a leap year\n" ;
else
std::cout << year << " is not a leap year\n" ;
}
}