This program illustrates a bit combination of logical and programming concepts of calender .And its easy to code and understand it you know the basics of C programming .In this program i have used some functions for just decorating the program output and might not work on turbo c++ compiler.And one more caution about this code when you compiler and run it in Turbo C++ compiler then it will give you logical error.It will calculate and give correct output in turbo till 170 years.So its better to use this program in other compiler like Dev C++,Codeblocks,Codelite etc.This is why because turbo C++ is very old and buggy.
Recommendation:use codeblocks or dev C++ IDE to make this program
Its coding time:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i, yy=0 ,l_days=0;
long int t_days=0 ;
system("cls");
system("color 1e"); //for changing background and text color
printf("\nCount total days till the given years");
printf("\nEnter the year::");
scanf("%d",&yy);
t_days=yy*365; //counts the days of the simple years till yy years
for(i=1; i<=yy; i++)
{
if(i%100==0) //condition for century
{
if(i%400==0) //condition for leap year in century
l_days++;
}
else
if(i%4==0) //condition for leap year
l_days++;
}
printf("\nleap years extra days::%d",l_days);
printf("\ntotal years days::%u",t_days);
t_days=t_days+l_days;
printf("\ntotal days till%d is::%u",yy,t_days);
getche();
return 0;
}
Recommendation:use codeblocks or dev C++ IDE to make this program
Its coding time:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int i, yy=0 ,l_days=0;
long int t_days=0 ;
system("cls");
system("color 1e"); //for changing background and text color
printf("\nCount total days till the given years");
printf("\nEnter the year::");
scanf("%d",&yy);
t_days=yy*365; //counts the days of the simple years till yy years
for(i=1; i<=yy; i++)
{
if(i%100==0) //condition for century
{
if(i%400==0) //condition for leap year in century
l_days++;
}
else
if(i%4==0) //condition for leap year
l_days++;
}
printf("\nleap years extra days::%d",l_days);
printf("\ntotal years days::%u",t_days);
t_days=t_days+l_days;
printf("\ntotal days till%d is::%u",yy,t_days);
getche();
return 0;
}
![]() |
output of the program |
Description :
In this program user will enter the year and this program will count the total day till that year and displays .system("cls") is used to clear screen like clrscr() function.
Logic:we have variable yy to take input from user and then we multiply yy to 365 so that we get the total days and store it in another variable t_days.But we have also leap years and their extra days to be added in the t_days to get net result.
Now the rest logic is on leapyear days .As we know the century that is divided by 400 will be the leapyear otherwise not.So in the for loop we check yy%100==0 ,if it is true then it is century and we then check whether divisible by 400 or not .I divisible by 400 then it is leap year and we increment the variable l_days by one.
The second part of the leap year,if it is not century and divided by 4 then it is leapyear and we increment the variable l_days by one.
At last we add all the days to get the net result like::total days=leap year extra days+total days by simple years.
I hope you understood the things.
we will come soon with another program so must visit again.