Write a C programme that prints the next 20 leap years. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a C programme that prints the next 20 leap years.

I can't understand, how I start this problem :")

13th Jun 2020, 5:25 AM
MD. ALVIN SARKAR SAKIB
MD. ALVIN SARKAR SAKIB - avatar
5 Answers
+ 7
Regardless of programming language, you need to know how to determine leap years. There is a simple mathematical formula for it. Check under "Gregorian calendar" on wikipedia. https://en.m.wikipedia.org/wiki/Leap_year
13th Jun 2020, 5:35 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks, for your opinion... I know this how I check a year which is leap year or not. But, I want to print next 20 leap years.How, I solve this problem..?
13th Jun 2020, 5:51 AM
MD. ALVIN SARKAR SAKIB
MD. ALVIN SARKAR SAKIB - avatar
+ 1
Start checking each number (starting this year or next year) If it is a leap year, print it If you have found 20 already, finish the program Move on to the next number. You can use a while loop, and an int variable to count how many years you found. If you know the formula then the rest is very simple, just put it into code. If you are still stuck, then post what you have tried so far.
13th Jun 2020, 5:57 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Yeah. Thanks a lot Martin Taylor .
13th Jun 2020, 6:37 PM
MD. ALVIN SARKAR SAKIB
MD. ALVIN SARKAR SAKIB - avatar
0
Is this code correct ? #include<stdio.h> int main() { int i,yr; printf("Enter year: "); scanf("%d",&yr); for(i=1;i<=20;) { if(yr%4==0 || yr%400==0 && yr%100==0) { printf("%d is a leap year\n",yr); i++; } yr+=1; } return 0; }
13th Jun 2020, 6:23 AM
MD. ALVIN SARKAR SAKIB
MD. ALVIN SARKAR SAKIB - avatar