This is a program to print the Julian day of a particular date.That is the no. of days that have lapsed in the year therefar. The syntax is right but he logic is not... so..some help? Also..I want some tips on how to make the code look cleaner.
#include<iostream.h>
int julian(int d, int m, int y);
int main()
{
int d,m,y;
cout<<"enter date(dd)";
cin>>d;
cout<<"enter month(mm)";
cin>>m;
cout<<"Enter year(yyyy)";
cin>>y;
int f=julian(d,m,y);
cout<<"Julian day="<<f;
}
int julian(int d, int m, int y)
{int t=0,leap=0;
for(int i=1;i<m;i++){
switch(i){ //adding days according to no. of months lapsed
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
t+=31;
break;
case 4:
case 6:
case 9:
case 11:
t+=30;
break;
case 2: //also checking for leap yr
t+=28;
if((y%4==0)&&(y%100!=0) ||(y%400==0))
leap++;
break;
}
t+=d;
if(t>59&&leap==1)
t++;
}
return t;
}
#include <iostream>
usingnamespace std;
int julian(int d, int m, int y);
int main()
{
int d,m,y;
cout<<"enter date(dd)";
cin>>d;
cout<<"enter month(mm)";
cin>>m;
cout<<"Enter year(yyyy)";
cin>>y;
int f=julian(d,m,y);
cout<<"Julian day="<<f;
return 0;
}
int julian(int d, int m, int y)
{
int t=0,leap=0;
for(int i=1;i<m;i++)
{
switch(i)
{ //adding days according to no. of months lapsed
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
t+=31;
break;
case 4:
case 6:
case 9:
case 11:
t+=30;
break;
case 2: //also checking for leap yr
t+=28;
if (((y%4==0)&&(y%100!=0)) ||(y%400==0))
leap++;
break;
}
}
t+=d;
if(t>59&&leap==1)
t++;
return t;
}
Notice also the use of
1 2
#include <iostream>
usingnamespace std;
instead of the deprecated <iostream.h>.
BTW, the leap variable and the condition on line 53 are not necessary. If you are counting the whole month and it is a leap year, just add 29 to t instead of 28 (or just put t++ on line 46.
I'll take care of my indentation . And, I also see your point in the avoidability of initiating the leap variable. Thanks. But as I mentioned my major problem is in the logic of the program which I request you guys to analyse.
Huh? What exactly do you mean by 'analyse the logic of the program'? That stuff about where to move the closing for loop brace and the (lack of) need for the leap variable wasn't enough for you?