Respuesta :

Answer:

The c++ program is given below.

#include <iostream>

using namespace std;

int main() {

   int yr;

   cout<<"Enter the year to test for leap year"<<endl;

   cin>>yr;

   do

   {

       if(yr<1000 || yr>9999)

       {

           cout<<"Invalid year. Enter a 4-digit value for the year"<<endl;

           cin>>yr;

       }        

   }while(yr<1000 || yr>9999);

   if((yr%4 == 0)||(yr%400 == 0))

       cout<<"true";  

   else if((yr%4!=0)&&(yr%100==0))

       cout<<"false";

   else

       cout<<"false";

   return 0;    

}  

OUTPUT

Enter the year to test for leap year

201

Invalid year. Enter a 4-digit value for the year

2019

false

Explanation:

The program is designed to accept only a four digit value. The input can be all possible values which is a four digit value.

The verification for the year entered by the user is done by if else statement. This test is put inside a do while loop. The testing is done for all input values.

The input should be a four digit value. If any other value is entered, the user is prompted till a valid year is entered.

   do

   {

       if(yr<1000 || yr>9999)

       {

           cout<<"Invalid year. Enter a 4-digit value for the year"<<endl;

           cin>>yr;

       }        

   }while(yr<1000 || yr>9999);

Leap year comes once in four years. This logic is implemented as follows.

The year if divisible by 4 or 400, it is a leap year.

Year is divisible by 4 or 400.

    if((yr%4 == 0)||(yr%400 == 0))

year is divisible by only 100.

if((yr%4!=0)&&(yr%100==0))

The program returns true if the user enters a leap year.

       cout<<"true";  

The year if only divisible by 100 and not divisible by 4, is not a leap year. Other values which do not fall in the above criteria of leap year is also not a leap year.

if((yr%4!=0)&&(yr%100==0))

The program returns false if the entered year is not a leap year.

cout<<"false";

The program structure can be different, i.e., instead of if else statements, the logic of divisibility can be implemented using other loops and statements as per the expertise of the programmer.

ACCESS MORE