Leap year finding – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Solved Problem #CPP#2008 siteicon   siteicon   siteicon  

Problem Statement - Leap year finding

Write a c++ program to take the value of a four digit year from the user and then print whether the given year is a leap year or not.


TC++ #2008


Run Output

Enter year (4 Digits) :2000
Given year 2000 is a leap year.

Enter year (4 Digits) :1900
Given year 1900 is not a leap year.

Enter year (4 Digits) :1996
Given year 1996 is a leap year.

int year,leap=0;
Hear we declare an integer variable year to collect value of year and also initialise a variable leap as 0 so that it can be used as a flag to be set as 1 when leap condition is true.

cout<<“Enter year (4 Digits) :”; cin>>year ;
Here we collect the 4 digit year value from the user.


if(year%400==0) leap=1;
This is a nested if within if to test the divisibility by 100 followed by divisibility by 400 to set the flag leap as 1. For century years (divisible by 100) like 1800, 1900, 2000 etc. the leap year is one which is divisible by 400.

else if(year%4==0) leap=1;
Other than the century years leap year has to be fully divisible by 4.

if(leap==1)  cout<<“Given year “<<year<<” is a leap year.”;
else cout<<“Given year “<<year<<” is not a leap year.”;
This simply printing of output based on condition of leap flag.



  • We can do the above program by not using the leap flag the program by directly outputting the message using cout when leap year condition is true. The use of leap flag helps to write the output message at one place. Using such flags is a better way of programming so that the similar messages are not written at multiple places.

Common Errors

  • If we do not use braces { } in the given code here, the dangling else problem may result in incorrect results.
    if(year%400==0) leap=1;
    else if(year%4==0) leap=1;


sunmitra| Created: 29-Dec-2017 | Updated: 18-Dec-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues