ch2-Leap year

Chapter_2     Exercise_2-1     Uninitialized_variables Exercise_2-2







leap.c     K&R, p. 41         download


#include <stdio.h> // for printf(), scanf()

int main()
{
int year;

printf("Year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
printf("%d is a leap year\n", year);
}
else {printf("%d is not a leap year\n", year);}

return 0;
}
/*
gcc leap.c -o leap
./leap
Year: 0
0 is a leap year

./leap
Year: 100
100 is not a leap year

./leap
Year: 400
400 is a leap year

./leap
Year: 2000
2000 is a leap year

./leap
2022
2022 is not a leap year

./leap
2023
2023 is not a leap year

./leap
Year: 2024
2024 is a leap year
*/









Chapter_2     Exercise_2-1     Uninit_vars BACK_TO_TOP Exercise_2-2



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo