Print all leap years from 1 to N
Posted on Apr 7, 2020
C program to print all leap years from 1 to N.
An year is said to be leap year if
year is a multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400). |
For example,
year = 1600,
1600 is divisible by both 100 and 400. So, 1600 is a leap year. |
year = 1700,
1700 is divisible by 100, but not divisible by 400. So, 1700 is not a leap year. |
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,year;
printf("Enter the value of N\t:");
scanf("%d",&n);
printf("Leap years from 1 to %d are\n",n);
for(i=1;i<=n;i++)
{
if((i%400==0)||(i%4==0 && i%100!=0))
printf("%d\t",i);
}
getch();
}
Output
Enter the value of N :1500
Leap years from 1 to 1500 are
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
64 68 72 76 80 84 88 92 96 104 108 112 116 120 124
128 132 136 140 144 148 152 156 160 164 168 172 176 180 184
188 192 196 204 208 212 216 220 224 228 232 236 240 244 248
252 256 260 264 268 272 276 280 284 288 292 296 304 308 312
316 320 324 328 332 336 340 344 348 352 356 360 364 368 372
376 380 384 388 392 396 400 404 408 412 416 420 424 428 432
436 440 444 448 452 456 460 464 468 472 476 480 484 488 492
496 504 508 512 516 520 524 528 532 536 540 544 548 552 556
560 564 568 572 576 580 584 588 592 596 604 608 612 616 620
624 628 632 636 640 644 648 652 656 660 664 668 672 676 680
684 688 692 696 704 708 712 716 720 724 728 732 736 740 744
748 752 756 760 764 768 772 776 780 784 788 792 796 800 804
808 812 816 820 824 828 832 836 840 844 848 852 856 860 864
868 872 876 880 884 888 892 896 904 908 912 916 920 924 928
932 936 940 944 948 952 956 960 964 968 972 976 980 984 988
992 996 1004 1008 1012 1016 1020 1024 1028 1032 1036 1040 1044 1048 1052
1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 1096 1104 1108 1112 1116
1120 1124 1128 1132 1136 1140 1144 1148 1152 1156 1160 1164 1168 1172 1176
1180 1184 1188 1192 1196 1200 1204 1208 1212 1216 1220 1224 1228 1232 1236
1240 1244 1248 1252 1256 1260 1264 1268 1272 1276 1280 1284 1288 1292 1296
1304 1308 1312 1316 1320 1324 1328 1332 1336 1340 1344 1348 1352 1356 1360
1364 1368 1372 1376 1380 1384 1388 1392 1396 1404 1408 1412 1416 1420 1424
1428 1432 1436 1440 1444 1448 1452 1456 1460 1464 1468 1472 1476 1480 1484
1488 1492 1496