0

Leap Year

I want to write a C Code where user could give a 4 Digit Year number like :2018 The programm will say if its leap or not leap year. But i want to add another step. Ex: if 2019 is not the leap year the programm shoudl tell me which year is next leap year. #include <stdio.h> int main() { int year; printf("Enter a year: "); scanf("%d", &year); // leap year if perfectly visible by 400 if (year % 400 == 0) { printf("%d is a leap year.", year); } // not a leap year if visible by 100 // but not divisible by 400 else if (year % 100 == 0) { printf("%d is not a leap year.\n", year); } // leap year if not divisible by 100 // but divisible by 4 else if (year % 4 == 0) { printf("%d is a leap year.\n", year); } // all other years are not leap year else { printf("%d is not a leap year.\n", year); } year+=1; printf("%d is the next leap year:\n", year); return 0; }

15th Nov 2020, 12:27 AM
Max Bauer
Max Bauer - avatar
10 Réponses
+ 2
You can just check if the year is divisible by 4, this is the only necessary and also sufficient condition to be a leap year. In case the year is not a leap year you can add to it (4- year%4) and you got the next leap year edit: oh I apologize for my ignorance, being divisible by 4 is not sufficient.
15th Nov 2020, 12:33 AM
Davide
Davide - avatar
+ 1
CarrieForle sorry I didn't know exactly about leap years till minutes ago and I was confused 🤯 Now the code should work right
15th Nov 2020, 1:24 AM
Davide
Davide - avatar
+ 1
if (yr % 400 == 0 || (yr % 100 != 0 && yr % 4 == 0)) { // Leap year } else { // Not a leap year // Next leap year Nly = yr + (4 - yr) % 4 }
15th Nov 2020, 1:27 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Davide you don't need make the leap year code as the question is to get the next leap year if year is not leap year.
15th Nov 2020, 1:28 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
I deleted the code because I am still confused 🙃 I will post it only if I am 100% sure
15th Nov 2020, 1:29 AM
Davide
Davide - avatar
+ 1
CarrieForle you are right, it's just that the ignorance took control of my brain
15th Nov 2020, 1:31 AM
Davide
Davide - avatar
+ 1
I had to make it, after all the confusion generated😔 #include <stdio.h> int main() { int 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\nThe next leap year is %d\n", year, year+4-year%4); } return 0; }
15th Nov 2020, 1:40 AM
Davide
Davide - avatar
0
First, you only need that when the year input is not a leap year, so you need another if statement to check the condition. Second, you can get the next leap year by adding 4 - year % 4 to the year. But there is an exception that a leap year is not divisible by 100, except if also divisible by 400, you need to increase the year by 4 if it is divisible by 100 and not divisible by 400.
15th Nov 2020, 1:05 AM
你知道規則,我也是
你知道規則,我也是 - avatar
- 2
Hello. I had to make it, after all the confusion generated😔 #include <stdio.h> int main() { int 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\nThe next leap year is %d\n", year, year+4-year%4); } return 0; } Here is your answer. If there is a spelling problem ingnore please Other I have put right format..... Thankyou
16th Nov 2020, 9:21 AM
Kunal Anand