Write a program to solve the date and days passed in a year

Write a program to solve the date and days passed in a year. There are two parts in this program; the first part will accept date to find passed days in the year. The second part uses the past days and year to find correct date in the year.

Solution

Answer:

//include header files

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

//declare arrays for months

int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};

//for leap-year

int b[12]={31,29,31,30,31,30,31,31,30,31,30,31};

//check leap-year

int checkleapYear(int y)

{   

     //Condition for a leap-year

     if(((y%4==0) ||(y%400==0)) &&(y%100!=0))

          //return 1

          return 1;

     //return o if not a leap-year

     return 0;

}

//Calculate the total days

void findDays(int m, int d, int y)

{

     //Check leap year

     int c=checkleapYear(y);

     int tdays=0;

     //For leap-year

     if(c==1)

     {

          //Calculate days

          for(int kk=0;kk<m-1;kk++)

          {

              //Add days

              tdays+=b[kk];

          }   

     }

     //for non-leap-year

     else

     {

          //calculate days  

          for(int kk=0;kk<m-1;kk++)

          {

              //Add days

              tdays+=a[kk];

          }   

     }

     //Add d

     tdays=tdays+d;

     //print total days

printf(\"\ There are %d days passed in the year %d\", tdays, y);

}

//method find date

void findDate(int tdays, int y)

{

     //Declare m

     int m=0;

     //Check for leap-year

     int c=checkleapYear(y);

     int kk=0;

     //For leap-year

     if(c==1)

     {

          //Find month

          while(tdays>b[kk])

          {

              //Imcremnt month

              m=m+1;

              //Reduce days

              tdays=tdays-b[kk];

              //Move to the next value

              kk++;

          }

     }

     //For non-leap-year

     else

     {

          //Find month

          while(tdays>a[kk])

          {

              //Increment the month

              m=m+1;

              //Reduce days

              tdays=tdays-a[kk];

              //Move to the next value

              kk++;

          }

     }

     //Increment month

     m+=1;

     //print date

     printf(\"\ The date is %d-%d-%d\", m,tdays,y);

}

//main

int main()

{

     //Declare c

     int c;

     char c1,c2=\'N\';

     int tdays, d,m,y;

     //Inifinite-loop

     do

     {

          //display program infor

printf(\"\ This program will find days passed or date in the year\");

printf(\"\ \\t 1) Input date(mm/dd/yyyy) to find days passed\");

printf(\"\ \\t 2) Input passed days to find date in the year\");

          printf(\"\ Your choice (1/2):\");

          scanf(\"%d\", &c);

          switch(c)

          {

              //find days passed

              case 1:

                   //get date

                   printf(\"\ Please input data (mm-dd-yy):\");

                   scanf(\"%d%c%d%c%d\", &m, &c1, &d, &c1, &y);

                   //Call findDays()

                   findDays(m,d,y);

                   break;

              //Find date

              case 2:

                   //Get total-days

                   printf(\"\ Input days:\");

                   scanf(\"%d\", &tdays);

                   //Get year

                   printf(\"\ Years:\");

                   scanf(\"%d\", &y);

                   //Call findDate()

                   findDate(tdays, y);

                   break;

              //Default

              default:

                   printf(\"INVALID\");

          }

          //Get user choice for repitition

          printf(\"\ Do more (Y/N) ?\");

          scanf(\"%c\", &c2);

          scanf(\"%c\", &c2);

         

     }while(c2!=\'N\');

     return 0;

}

sample output:

sh-4.3$ gcc -o main *.c                                                                                                           

sh-4.3$ main                                                                                                                      

                                                                                                                                   

This program will find days passed or date in the year                                                                           

         1) Input date(mm/dd/yyyy) to find days passed                                                                             

         2) Input passed days to find date in the year                                                                            

Your choice (1/2):1                                                                                                               

Please input data (mm-dd-yy):12-31-1981

                                                                                                                                  

There are 365 days passed in the year 1981                                                                                        

Do more (Y/N) ?Y                                                                                                                 

                                                                                                                                   

This program will find days passed or date in the year                                                                           

         1) Input date(mm/dd/yyyy) to find days passed                                                                            

         2) Input passed days to find date in the year                                                                            

Your choice (1/2):2                                                                                                               

                                                                                                                                  

Input days:78                                                                                                                     

                                                                                                                                  

Years:1981                                                                                                                        

                                                                                                                                  

The date is 3-19-1981                                                                                                            

Do more (Y/N) ?N                                                                                                                 

sh-4.3$   

 Write a program to solve the date and days passed in a year. There are two parts in this program; the first part will accept date to find passed days in the ye
 Write a program to solve the date and days passed in a year. There are two parts in this program; the first part will accept date to find passed days in the ye
 Write a program to solve the date and days passed in a year. There are two parts in this program; the first part will accept date to find passed days in the ye
 Write a program to solve the date and days passed in a year. There are two parts in this program; the first part will accept date to find passed days in the ye
 Write a program to solve the date and days passed in a year. There are two parts in this program; the first part will accept date to find passed days in the ye
 Write a program to solve the date and days passed in a year. There are two parts in this program; the first part will accept date to find passed days in the ye

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site