Đếm năm nhuận C++

Lấy hai năm làm đầu vào từ người dùng và hiển thị tất cả năm nhuận giữa hai năm đầu vào này theo người dùng



Bấm vào đây để mở chương trình này trong Turbo C++

/*******************************************************
Statement - Check leap year in a range.
Programmer - Vineet Choudhary
Written For - http://developerinsider.co
*******************************************************/

#include <stdio.h>
#include <conio.h>

void main(){
    int year;
    int min_year,max_year;
    clrscr();

    printf("Enter the lowest year: ");
    scanf("%d",&min_year);

    printf("Enter the heighest year: ");
    scanf("%d",&max_year);

    printf("Leap years in given range are: ");
    for(year = min_year;year <= max_year; year++){
         if(((year%4==0)&&(year%100!=0))||(year%400==0))
             printf("%d ",year);
    }
  
    getch();
}

/*

Definition of leap year:
------------------------
Rule 1: 
-------
A year is called leap year if it is divisible by 400.
For example:
------------
1600, 2000 etc leap year while 1500, 1700 are not leap year.

Rule 2: 
-------
If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year.
For example:
------------
2004, 2008, 1012 are leap year.


Leap year logic or Algorithm of leap year or Condition for leap year:
---------------------------------------------------------------------
IF year MODULER 400 IS 0
 THEN leap_year
ELSE IF year MODULER 100 IS 0
 THEN not_leap_year
ELSE IF year MODULER 4 IS 0
 THEN leap_year
ELSE
 not_leap_year

*/

Để tính xem một năm có phải là năm nhuận hay không, hãy bắt đầu bằng cách kiểm tra xem số đó có chia hết cho 4 hay không vì cần phải có năm nhuận. Sau đó, nếu nó chia hết cho 4, hãy kiểm tra xem nó có chia hết cho 100 không. Nếu không, thì đó là một năm nhuận. Tuy nhiên, nếu nó chia hết cho cả 100 và 4 thì vẫn có thể là năm nhuận. Để kiểm tra xem năm có chia hết cho 400 không. Nếu đúng thì là năm nhuận. Để tìm hiểu cách kiểm tra xem một năm có phải là năm nhuận hay không bằng cách sử dụng lịch, hãy cuộn xuống

Bản tóm tắt này có giúp ích gì cho bạn không? Có Không

Cảm ơn tất cả các tác giả đã tạo ra một trang đã được đọc 603,251 lần

Leap Year
6
1
1
1
2

1
3
1
4
1
5
1
6

 

Leap Year
6
1
8

Leap Year
6
Leap Year
0

Leap Year
6
1
1
Leap Year
3

1
3____24
Leap Year
6____26

 

Leap Year
6
Leap Year
9

Leap Year
6
Leap Year
9

Leap Year
6
1
1
Leap Year
4

1
3
1
4
1
5
1
6

Leap Year
6
1
4
Leap Year
6_______26

1
013

 

1
014

Leap Year
3
1
016

Leap Year
5

Leap Year
6
Leap Year
3
1
040

 

Leap Year
6____9042______9043
1
044

1
045
1
046
1
047
1
6

Leap Year
6
1
4
1
111

1
013

 

1
113

1
114

C




1
115

1
04

1
117

1
118

 

Leap Year
1
Leap Year
2
Leap Year
3
Leap Year
4

Leap Year
5

Leap Year
6
Leap Year
7

Leap Year
6
Leap Year
9

Leap Year
6
1
1
1
2

1
3
1
4
1
5
1
6

 

Leap Year
6
1
8

Leap Year
6
Leap Year
0

Leap Year
6
1
1
Leap Year
3

1
3____24
Leap Year
6____26

 

Leap Year
6
Leap Year
9

Leap Year
6
Leap Year
9

Leap Year
6
1
1
Leap Year
4

1
3
1
4
1
5
1
6

Leap Year
6
1
4
Leap Year
6_______26

1
013

 

Leap Year
22

Leap Year
3
1
016

Leap Year
5

Leap Year
6
Leap Year
3
1
040

 

Leap Year
6
Leap Year
30
Leap Year
31
Leap Year
32
1
043
Leap Year
34

Leap Year
35
Leap Year
31____132
1
047
Leap Year
39

Leap Year
6
1
4
1
111

1
013

Java




Leap Year
44

Leap Year
45

Leap Year
6

Leap Year
47
Leap Year
48

Leap Year
5

Leap Year
6
Leap Year
51
Leap Year
52
Leap Year
2
Leap Year
3
Leap Year
4

Leap Year
6
Leap Year
5

1
3____17

1
3____19

1
3
1
1
Leap Year
64
Leap Year
65
Leap Year
66
Leap Year
67
Leap Year
68

Leap Year
69
1
4
1
5
1
6

Leap Year
6

1
3____28

1
3____10

1
3_______21
Leap Year
64____181
Leap Year
66
Leap Year
67
Leap Year
68

Leap Year
69
1
4
Leap Year
6_______26

Leap Year
6

1
3____19

1
3____19

1
3
1
1
Leap Year
64
Leap Year
97
Leap Year
66
Leap Year
67
Leap Year
68

Leap Year
69
1
4
1
5
1
6

1
3____24
Leap Year
6____26

Leap Year
6____9013

1
3

Leap Year
6
1
13

Leap Year
6
1
15
Leap Year
51
1
17
1
18

Leap Year
6
Leap Year
5

1
3______13
1
23____224
1
6

1
3______227____224
1
29
1
043
1
044

1
32____9047
Leap Year
39

Leap Year
6____9013

1
013

Python3




1
38

1
39
1
40

Leap Year
6
1
1
1
43
1
44
Leap Year
97
Leap Year
68
1
47
1
47
Leap Year
67
1
044

1
3
1
1
1
43
1
44
Leap Year
81
Leap Year
68
1
47
1
47
Leap Year
67
1
044

Leap Year
69
1
1
1
43
1
44
Leap Year
65
Leap Year
68
1
47
1
47
Leap Year
67
1
044

1
71
1
4
1
73

Leap Year
69
1
75____9044

1
71
1
4
1
79

1
3______275____9044

1
83
1
4
1
73

Leap Year
6
1
75____9044

1
3____24
1
79

 

1
92

1
93____247
1
24

1
1____297

Leap Year
6
1
99____132
1
043
Leap Year
68

1
75____9044

Leap Year
6
1
99____132
1
047
Leap Year
68

Leap Year
6

Leap Year
11

C#




Leap Year
12

Leap Year
45

1
41
Leap Year
15

 

Leap Year
47
Leap Year
17

Leap Year
5

Leap Year
6

Leap Year
6
Leap Year
51
Leap Year
1
Leap Year
2_______13
Leap Year
4

Leap Year
6
Leap Year
5

1
3____17

1
3____19

1
3
1
1
1
2

Leap Year
69
1
4
1
5
1
6

Leap Year
6

1
3____28

1
3____10

1
3____21
Leap Year
3

Leap Year
69
1
4
Leap Year
6_______26

Leap Year
6

1
3____19

1
3____19

1
3____21
Leap Year
4

Leap Year
69
1
4
1
5
1
6

1
3____24
Leap Year
6____26

Leap Year
6____9013

1
3

Leap Year
6
1
13

Leap Year
6
1
15
Leap Year
51
1
17
Leap Year
76

Leap Year
6
Leap Year
5

1
3
Leap Year
3
1
040

1
3____183____9043
1
044

Leap Year
86____9047
Leap Year
39

Leap Year
6____9013

 

1
013

 

Leap Year
92

PHP




Leap Year
93

Leap Year
94

Leap Year
95

 

Leap Year
96
Leap Year
2____198
Leap Year
68

Leap Year
5

Leap Year
6
Leap Year
7

Leap Year
6
Leap Year
9

Leap Year
6
1
1
Leap Year
32____198
Leap Year
09

1
3____299____132
1
043
Leap Year
39

1
3

Leap Year
6
1
8

Leap Year
6
Leap Year
0

Leap Year
6
1
75
1
1
Leap Year
32
Leap Year
98
Leap Year
25

1
3____299____132
1
047
Leap Year
39

Leap Year
69

Leap Year
6
Leap Year
9

Leap Year
6
Leap Year
9

Leap Year
6
1
75
1
1
Leap Year
32
Leap Year
98
Leap Year
41

1
3____299____132
1
043
Leap Year
39

1
3

Leap Year
6
1
75

1
3____299____132
1
047
Leap Year
39

1
013

 

1
014

Leap Year
98
Leap Year
58

 

Leap Year
2____198____139

Leap Year
6

Leap Year
63

Leap Year
64

Javascript




Leap Year
65

 

Leap Year
66

Leap Year
45

 

Leap Year
6
Leap Year
96
Leap Year
70

1
3____17

1
3____19

1
3
1
1
1
2

Leap Year
69
1
4
1
5
1
6

 

1
3____28

1
3____10

1
3____21
Leap Year
3

Leap Year
69
1
4
Leap Year
6_______26

 

1
3____19

1
3____19

1
3____21
Leap Year
4

Leap Year
69
1
4
1
5
1
6

1
3____24
Leap Year
6____26

Leap Year
6____9013

 

Leap Year
6
1
13

1
0112

1
3____90114

1
3______90116______9043
1
044
1
047
Leap Year
39

 

 

1
0121

 

1
0122

đầu ra

Leap Year

Thời gian phức tạp. Ô(1)

Không gian phụ trợ. Ô(1)

ví dụ 2. Mã một dòng để kiểm tra xem năm đó có phải là năm nhuận hay không

C++




1
0123

1
0124

1
11

1
41
1
68
Leap Year
0

 

Leap Year
1
Leap Year
2
Leap Year
3
Leap Year
4

Leap Year
5

Leap Year
6

Leap Year
6____90136

Leap Year
6____90138

Leap Year
6____90140

Leap Year
6
1
4
1
0143

1
83____90145

1
013

 

1
014

Leap Year
3
1
016

Leap Year
5

Leap Year
6
Leap Year
3
1
040

 

Leap Year
6____90155______9043
1
044

1
0158
1
046
1
047
1
6

Leap Year
6
1
4
1
111

1
013

 

1
0166

C




1
0123

1
0124

1
117

1
118

 

Leap Year
1
Leap Year
2
Leap Year
3
Leap Year
4

Leap Year
5

1
0136

1
0138

1
0140

1
4
1
0143

1
3____90145

1
013

 

Leap Year
22

Leap Year
3
1
016

Leap Year
5

Leap Year
6
Leap Year
3
1
040

 

Leap Year
6
Leap Year
30
Leap Year
31
Leap Year
32
1
043
Leap Year
34

1
71
Leap Year
31
Leap Year
32
1
047
Leap Year
39

Leap Year
6
1
4
1
111

1
013

Java




Leap Year
44

Leap Year
45

Leap Year
6

Leap Year
47
Leap Year
48

Leap Year
5

Leap Year
6
Leap Year
51
Leap Year
52
Leap Year
2
Leap Year
3
Leap Year
4

Leap Year
6
Leap Year
5

Leap Year
6____90136

Leap Year
6____90423

Leap Year
6____90140

Leap Year
6
1
4
1
0428
Leap Year
97
Leap Year
66
Leap Year
67
1
0432
Leap Year
81
1
0434
Leap Year
67
1
0436

Leap Year
69
Leap Year
64
Leap Year
65
Leap Year
66
Leap Year
67
1
0442

Leap Year
6____9013

1
3

Leap Year
6
1
13

Leap Year
6
1
15
Leap Year
51
1
17
1
18

Leap Year
6
Leap Year
5

1
3______13
1
23____224
1
6

1
3______90461
1
24
1
29
1
043
1
044

1
32____9047
Leap Year
39

Leap Year
6____9013

1
013

Python3




1
0472

1
0473

 

1
39
1
40

 

Leap Year
6____90477

Leap Year
6____90479

Leap Year
6____90481

Leap Year
6
1
4
1
0484
1
44
Leap Year
97
1
47
1
47
Leap Year
67
Leap Year
68
1
0491
1
43
1
44
Leap Year
81
1
0495
1
47
Leap Year
67
1
0498
1
0499
1
43
1
44
Leap Year
65
1
47
1
47
Leap Year
67___90442

 

1
92

1
93____247
1
24

1
1____297

Leap Year
6
1
99____132
1
043
Leap Year
68

1
75____9044

Leap Year
6
1
99____132
1
047
Leap Year
68

Leap Year
6

Leap Year
11

C#




Leap Year
12

Leap Year
45

1
41
Leap Year
15

 

Leap Year
47
Leap Year
17

Leap Year
5

Leap Year
6
Leap Year
51
Leap Year
1
Leap Year
2_______13
Leap Year
4

Leap Year
6
Leap Year
5

1
3____90136

1
3____90423

1
3____90140

1
3____24
1
0143

1
71____90145

Leap Year
6____9013

1
3

Leap Year
6
1
13

Leap Year
6
1
15
Leap Year
51
1
17
Leap Year
76

Leap Year
6
Leap Year
5

1
3
Leap Year
3
1
040

1
3____183____9043
1
044

Leap Year
86____9047
Leap Year
39

Leap Year
6____9013

 

1
013

Leap Year
92

PHP




Leap Year
93

Leap Year
94

Leap Year
95

 

Leap Year
96
Leap Year
2____198
Leap Year
68

Leap Year
5

Leap Year
6

Leap Year
6____90136

Leap Year
6____90138

Leap Year
6____90140

Leap Year
6
1
4
1
1196______198
1
1198

1
83
Leap Year
32____198
1
4102

1
83
Leap Year
32____198
1
4106

1
013

 

1
014

Leap Year
98
Leap Year
58

 

Leap Year
2______198____229
1
99
Leap Year
32
1
043
Leap Year
34

1
4118
1
99
Leap Year
32
1
047
Leap Year
39

 

Leap Year
63

Leap Year
64

Javascript




Leap Year
65

1
4126

Leap Year
45

Leap Year
6
Leap Year
96
1
4130

Leap Year
6
Leap Year
5

Leap Year
6

1
3____90136

1
3____90423

1
3____90140

1
3____24
1
4142

Leap Year
6____9013

 

Leap Year
6
1
13

Leap Year
6____94148
1
040

Leap Year
6
1
0116______9043
1
044
1
047
Leap Year
39

 

 

1
4156

1
0122

đầu ra

Leap Year

Thời gian phức tạp. Ô(1)

Không gian phụ trợ. Ô(1)

ví dụ 3. Kiểm tra năm nhuận bằng Macro trong C/C++

C++




1
4158

1
4159

1
4160

 

1
4161

1
41
1
68
Leap Year
0

 

1
4165

1
4166

1
4167

1
4168

1
4169

1
4170

Leap Year
3
1
016

Leap Year
5

Leap Year
6
Leap Year
3
1
4176

Leap Year
6
1
4178
1
4179
1
6

Leap Year
6
1
4
1
111

1
013

Java




1
4185

1
4159

1
4160

1
15
Leap Year
47
1
4190

Leap Year
5

1
4192____94165

1
4192____94166

1
4192
Leap Year
51
Leap Year
3
1
4199
Leap Year
3
1
6801

1
4192
Leap Year
5

Leap Year
6
1
1____96806
Leap Year
65
Leap Year
66
Leap Year
67
1
6810

1
4169
1
6812
Leap Year
81
1
0434
Leap Year
67
1
6816

1
4169
1
6812
Leap Year
97
Leap Year
66
Leap Year
67
1
0498

Leap Year
6
Leap Year
5

1
6825
1
4
1
6827
1
6

Leap Year
6____9013

Leap Year
6
1
75

Leap Year
6
Leap Year
5

1
6825
1
4
Leap Year
67______26

Leap Year
6____9013

1
4192____9013

 

1
4192____9014

1
4192
1
15
Leap Year
51
1
17
1
18

1
4192
Leap Year
5

Leap Year
6
Leap Year
3
1
23____96855
1
6

Leap Year
6____96858

1
4192____9013

1
013

 

1
6862

Python3




1
6863

1
6864

1
6865

 

1
6866

1
6867

1
39
1
6869

1
4192
1
1______96872
1
44
Leap Year
65
1
47
1
47
Leap Year
67
Leap Year
68
1
0499

1
0112
1
6881
1
44
Leap Year
81
1
0495
1
47
Leap Year
67
Leap Year
68
1
0491

1
0112
1
6881
1
44
Leap Year
97
1
47
1
47
Leap Year
67
1
6896

Leap Year
6
1
4
1
6827
1
6

1
4192____275____9044

Leap Year
6
1
4
Leap Year
67
1
6

 

Leap Year
008

1
1
Leap Year
010
1
47
1
47
Leap Year
013
1
044

 

1
4192
1
93
1
47
1
6855
1
6

1
4192
1
99____1022

 

1
4192
Leap Year
024

C#




Leap Year
025

1
4159

1
4160

1
41
Leap Year
15

Leap Year
47
Leap Year
031

Leap Year
6

1
4192____94165

1
4192____94166

1
4192
Leap Year
51
Leap Year
3
1
4199
Leap Year
3
1
6801

1
4192
Leap Year
5

Leap Year
6
1
1
Leap Year
047

1
4169
Leap Year
049

1
4169
Leap Year
051

Leap Year
6
Leap Year
5

________ 96825 ________ 24 ________ 1056

Leap Year
6____9013

Leap Year
6
1
75

Leap Year
6
Leap Year
5

1
6825
1
4
1
111

Leap Year
6____9013

1
4192____9013

1
4192

1
4192____9014

1
4192
Leap Year
51
1
17
Leap Year
76

1
4192
Leap Year
5

Leap Year
6
Leap Year
3
1
4176

Leap Year
6
Leap Year
083

1
4192____9013

1
013

 

Leap Year
087

Javascript




Leap Year
65

Leap Year
089

1
4159

1
4160

 

1
4192____94165

1
4192____94166

1
4192
Leap Year
96
Leap Year
098

1
4192
Leap Year
5

Leap Year
6
1
1
Leap Year
047

1
4169
Leap Year
049

1
4169
Leap Year
051

Leap Year
6
Leap Year
5

________ 96825 ________ 24 ________ 1056

Leap Year
6____9013

Leap Year
6
1
75

Leap Year
6
Leap Year
5

1
6825
1
4
1
111

Leap Year
6____9013

1
4192____9013

 

1
4192____9014

1
4192
1
4148
1
4176

1
4192____1132

1
4192

Leap Year
134

1
0122

đầu ra

1

Thời gian phức tạp. Ô(1)

Không gian phụ trợ. Ô(1)

Giải trình

Chương trình xuất ra 1 nếu năm nhuận và 0 nếu không phải năm nhuận

Ví dụ 4. Giải pháp ngắn trong Python

con trăn




1
39
1
40

Leap Year
138

Leap Year
6____90477

Leap Year
6____90479

Leap Year
6____90481

Leap Year
6
Leap Year
146
Leap Year
147

Leap Year
6
1
4
Leap Year
150

Leap Year
138

Leap Year
152

1
93____247
1
24

1
1
1
97

Leap Year
6
1
99____132
1
043
Leap Year
68

1
75____9044

Leap Year
6
1
99____132
1
047
Leap Year
68

1
4169

Leap Year
171

đầu ra

Leap Year

Thời gian phức tạp. Ô(1)

Không gian phụ trợ. Ô(1)

Ví dụ 5. Giải pháp ngắn gọn trong Java sử dụng phương thức isLeap() của lớp Year

Java




Leap Year
172

Leap Year
173

 

Leap Year
146
Leap Year
175

Leap Year
146
Leap Year
177

 

Leap Year
47
Leap Year
031

Leap Year
6

Leap Year
6____1182

Leap Year
6
1
15
Leap Year
51
Leap Year
52
Leap Year
2
Leap Year
3
Leap Year
189

________ 23 ________ 1191 ________ 1192

1
3____24
Leap Year
195

Leap Year
6____9013

Leap Year
6

Leap Year
6
Leap Year
200

Leap Year
6
1
15
Leap Year
51
1
17
Leap Year
205

1
3______13
1
23____224
1
6
Leap Year
211

1
3____21
Leap Year
214
Leap Year
215

Leap Year
69
Leap Year
217____9043
Leap Year
39

1
3______9013____275
Leap Year
5

Leap Year
69
Leap Year
217____9047
Leap Year
39

1
3____9013

Leap Year
6____9013

1
013

 

Leap Year
233

đầu ra

Leap Year

Thời gian phức tạp. Ô(1)

Không gian phụ trợ. Ô(1)

Giải trình

Lớp năm trong java là một lớp dựng sẵn được sử dụng để biểu thị một năm dưới dạng một đối tượng không thay đổi ngày giờ. Ý tưởng là gọi phương thức isLeap() của lớp trả về giá trị true nếu năm đó là năm nhuận và ngược lại

Năm 2032 có phải là năm nhuận không?

Năm nhuận là 4 hay 7 năm một lần?

Thông thường, một năm nhuận xảy ra cứ bốn năm một lần , may mắn thay, đây là một quy luật khá đơn giản để ghi nhớ. Tuy nhiên, có một chút nhiều hơn thế. Dưới đây là quy luật của năm nhuận. Một năm có thể là năm nhuận nếu nó chia hết cho 4.

Năm 2022 có phải là năm nhuận không?

Tin xấu, năm nay không phải là năm nhuận vì chỉ là năm 2022 , nhưng tin tốt là năm nhuận tiếp theo là . Từ thời điểm đó, các năm nhuận tiếp theo sẽ là 2028, 2032 và 2036.

Năm nào sau đây là năm nhuận * A 2000 B 2018 C 2200?

Giải pháp chi tiết . Vậy năm 2000 là năm nhuận. Do đó, câu trả lời đúng là c) 2000 .