Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i want a help with typing a c program , it wrd the output is the date after n days for example (1/1/2000)n=3--->the output is

What I have tried:

C++
int main()lid date");
}
printf("Enter number of days\n");
scanf("%d",&n);

  return 0;
}


Added code from comment:
C
int main()
{
  int day=0;
  int month=0;
  int year=0;
  int n=0;
  printf("Enter the date\n");
  scanf("%d/%d/%d",&day,&month,&year);
  if(month>=1&&month<=12)
  {
    if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
    {
      if(day>=1&&day<=31)
      {
        printf("%d/%d/%d\n",day,month,year);
      }else{
        printf("Invalid date");
      }
    }
    else if(month==4||month==6||month==9||month==11)
    {
      if(day>=1&&day<=30)
      {
        printf("%d/%d/%d\n",day,month,year);
      }else{
        printf("Invalid date");
      }
    }
    else if(month==2)
    {
      if(year%4==0)
      {
        if(year%100==0)
        {
          if(year%400==0)
          {
            if(day>=1&&day<=29)
            {
              printf("%d/%d/%d\n",day,month,year);
            }else{
              printf("Invalid date.\n");
            }
          }
          else if(day>=1&&day<=28)
          {
            printf("%d/%d/%d\n",day,month,year);
          }else{
            printf("Invalid date");
          }
        }
        else if(day>=1&&day<=29)
        {
          printf("%d/%d/%d\n",day,month,year);
        }else{
          printf("Invalid date");
        }
      }
      else if(day>=1&&day<=28)
      {
        printf("%d/%d/%d\n",day,month,year);
      }else{
        printf("Invalid date");
      }
    }
  }
  else{
    printf("Invalid date");
  }
  printf("Enter number of days\n");
  scanf("%d",&n);
  return 0;
}
Posted
Updated 26-Aug-21 20:38pm
v5
Comments
[no name] 7-Apr-16 15:02pm    
anyone plz؟
[no name] 7-Apr-16 16:08pm    
plz anyone can help me :( i am sorry for asking for help like that but i need to submit this maximum after two hours ...so yeahhhh

First you need to learn the syntax of C.
What you have shows us that you don't really understand some pretty basic concepts in C.
For example:
C++
if(month=1,3,5,7,8,10,12){

will assign the value 1 to month and will always take the branch.
This is not how to check for one of a set of values.

You seem to think that = is the equality comparison operator and that <= is assignment. Neither is correct!
 
Share this answer
 
Comments
Matt T Heffron 7-Apr-16 15:17pm    
In the future, don't put updated code in a "Reply".
It's pretty unreadable here.
Use the "Improve question" on the original question to make changes.
I've done it for you this time.
Matt T Heffron 7-Apr-16 15:28pm    
The way I'd do this would be to convert the input date to an integer representing the number of days since the beginning of some reference year.
(You could calculate days since the beginning of year 1, there was no year 0, but that assumes the current calendar system back before it was in use!)
Then add the number of days offset (n) and convert back to day, month, year. The hardest part of converting back may be determining how many leap years to account for.
Matt T Heffron 7-Apr-16 15:40pm    
Give me a bit of time to compose a response... It'll take a short while!
Matt T Heffron 7-Apr-16 16:15pm    
OK, for staters, I'm assuming this is some form of coursework (since there's no other obvious reason to do this) so I'm not going to just give you the solution, but I'll try to guide you through it. (It would be nearly trivial in C#!)

The code you have "parses" the input date into day, month, and year.
(It's not pretty but it looks like it works.)
You would need to choose a reference year, say 1600, and then invalidate the input if the user gives an earlier date than 1/1/1600.
Now you need a variable to hold the calculated total days since the reference year.
(A 32 bit int will allow for up to 5.8 million years!)
First subtract 1600 from the input year value into years.
Calculate the number of "potential" leap years in that value: years / 4
Put 366 1461 (= 366 + 3*365) times that into the total days variable.
Then figure out how many century years there were: years / 100
Subtract that number from the total days (those weren't leap years).
Then add back in for the years divisible by 400: years / 400
At this point you have the number of days from 1/1/1600 to just before the user input year.
Now you need to add in the appropriate number of days to the beginning of the user input month.
If the month is after February, then account for year being a leap year.
Then add in the input day value.
This is now the total number of days from 1/1/1600 to the input date (inclusive)!!

Now you can just add the days offset (n) to the total.
Then you can "unwind" the calculation above to get the new day, month, year.
[no name] 7-Apr-16 15:40pm    
oh no .. i think this won't work if n was a big number :(
Maybe something in this[^] link will help.
 
Share this answer
 
Comments
[no name] 7-Apr-16 15:56pm    
it didn't help bacause i have not learned all of this ... i am still in the basics
[no name] 7-Apr-16 15:58pm    
do you have an idea how i can solve this thing that i have been stuck in for a weak lol
Create a date data structure(day, month, and year). WAP to add or substract a user entered
number of days from it and print the resultant date in terms of day, month, and year.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900