Friday, April 04, 2014

Leap Year Check - C Program

Problem Question


Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.

Explanation of Problem


We want our program to accept user’s input, an integer which shall represent a year. Our program should check whether or not it is a leap year. For an year to be leap, it should be divisible by 4 and not divisible by 400.

Code


#include <stdio.h>
/*LEAP CHECK*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: CodeBlocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 04-04-2014**/
int main()
{
  int year;
  printf("\n\nEnter an year: ");
  scanf("%d", &year);
  if (!(year % 4) && (year % 400))
    printf("\n\nLEAP\n\n");
  else
    printf("\n\nNOT LEAP\n\n");
  system("pause");
  return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

(!(year % 4) && (year % 400)) -> This is the main logic of the program. ‘year’ stores the value user entered. If we wish that the number entered represent a leap year, then ‘year%4’ must be zero (year is divisible by 4), and ‘year%400’ should not be zero (year is not divisible by 400). I used a ‘!’ (LOGICAL NOT) on the first condition, and used ‘&&’ (LOGICAL AND) between the two conditions of checking the divisibility of the variable ‘year’. LOGICAL AND helps in making sure that when both the conditions are true (1. year is divisible by 4; 2. Year is not divisible by 400), the value of year represents a LEAP year.
To explain the logic I have used, consider a few cases:
  1. year=1998: In this case, let’s analyse our condition, (!(year % 4) && (year % 400)). For the first part, !(year % 4), year%4 returns a non-zero value since 1998 is not divisible by 4. Thus, !(year%4) turns out to be zero. Since it is followed by &&, and the first condition is denoting false, the program won’t check divisibility by 400 condition. Hence, NOT LEAP will be printed on screen.
  2. year=2000: In this case, let’s analyse our condition, (!(year % 4) && (year % 400)). For the first part, !(year % 4), year%4 returns a zero since 2000 is divisible by 4. Thus, !(year%4) turns out to be non-zero. Since it is followed by &&, and the first condition is denoting true, the program would check divisibility by 400 condition. On checking the second condition, (year % 400), we see that 2000 is divisible by 400, and hence (year % 400) returns 0. Now && is applied like, 1 && 0, thus, returning zero. Hence, NOT LEAP will be printed on screen.
  3. year=2004: In this case, let’s analyse our condition, (!(year % 4) && (year % 400)). For the first part, !(year % 4), year%4 returns a zero since 2004 is divisible by 4. Thus, !(year%4) turns out to be non-zero. Since it is followed by &&, and the first condition is denoting true, the program would check divisibility by 400 condition. On checking the second condition, (year % 400), we see that 2004 is not divisible by 400, and hence (year % 400) returns non-zero value. Now && is applied like, 1 && 1, thus, returning 1. Hence, LEAP will be printed on screen.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)



No comments:

Post a Comment

Need help?