A programme in C++ that if date is input it tells day?
For example
Enter Date : 1-01-1985
The day is 'Monday'

Enter Date : 2-01-1985
The day is 'Tuesday'

Restriictions are using If-else, switch,operators(/,%,*,+,-) while loop.logical operations(&& || )
Please write the programme or Explain the logic
It starts like this.
if (year%4==0)
"it is leap year"
Then what?????
PLEASE HELP.
THIS IS MY FINAL DESTINATION.If no answer from here. I won't find it how to do!!!
Thanx

Edward doesn't like just bumping people off to another website, but perhaps this is what you're looking for?

It is great but there is some confusion

W = (k + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) + floor(C/4)) mod 7
where floor() denotes the integer floor function,
k is day (1 to 31)
m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) Treat Jan & Feb as months of the preceding year
C is century (1987 has C = 19)
Y is year (1987 has Y = 87 except Y = 86 for Jan & Feb)
W is week day (0 = Sunday, ..., 6 = Saturday)
IN THIS FORMULA what is floor() denotes the integer floor function,
I have not learned that? What else can I use??
If-else, switch,operators(/,%,*,+,-) while loop.logical operations(&& || )
CAN U PLEASE WRITE SOMETHING I HAve ONLY ONE DAY?????
PLEASE!PLEASE!!!!!!!PLEEEEEEEASe
THANX.

It is great but there is some confusion

W = (k + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) + floor(C/4)) mod 7
where floor() denotes the integer floor function,
k is day (1 to 31)
m is month (1 = March, ..., 10 = December, 11 = Jan, 12 = Feb) Treat Jan & Feb as months of the preceding year
C is century (1987 has C = 19)
Y is year (1987 has Y = 87 except Y = 86 for Jan & Feb)
W is week day (0 = Sunday, ..., 6 = Saturday)
IN THIS FORMULA what is floor() denotes the integer floor function,
I have not learned that? What else can I use??
If-else, switch,operators(/,%,*,+,-) while loop.logical operations(&& || )
CAN U PLEASE WRITE SOMETHING I HAve ONLY ONE DAY?????
PLEASE!PLEASE!!!!!!!PLEEEEEEEASe
THANX.

I can't make any sense of this post. I don't know what you wrote, what your professor wrote, what comes from the link from Radical Edward, and what exactly you are asking. Stop begging. It will get you nowhere. You've given no context for this formula:

W = (k + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) + floor(C/4)) mod 7

Where does it come from? I can give some guesses as to what most of it means, but not all of it. Taking the "floor" of a number is simply rounding it down to an integer. The floor of 4.7 would be 4.
http://www.cplusplus.com/reference/clibrary/cmath/floor.html

I don't know whether you need to use these formulas or not. The ctime library mightn come in handy for you. In particular the struct tm time structure.
http://www.cplusplus.com/reference/clibrary/ctime/
http://www.cplusplus.com/reference/clibrary/ctime/tm.html

Post what you know how to do. You can at least read in the data and pass it to a function that will give you whatever information you want from that date.

It's best to use the standard floor function, but you can fake the effect somewhat by converting to an integer and then back to truncate any precision:

#include <iostream>
#include <cmath>

namespace Edward {
  double floor(double value)
  {
    return (double)(int)value;
  }
}

int main()
{
  for (double i = 1.0; i < 5.0; i += .1)
    std::cout << i << '\t' << std::floor(i)
      << '\t' << Edward::floor(i) << '\n';
}

That's an alternative if you're not allowed to use a standard function that hasn't been taught in class yet, because the trick only uses casting. If you can't use casting, you can do the same thing with implicit conversions and all you'll get is a warning about converting double to int:

#include <iostream>
#include <cmath>

namespace Edward {
  double floor(double value)
  {
    int temp = value;
    return temp;
  }
}

int main()
{
  for (double i = 1.0; i < 5.0; i += .1)
    std::cout << i << '\t' << std::floor(i)
      << '\t' << Edward::floor(i) << '\n';
}

Well , There is another solution which is simple but requires somewhat more code. But it is sure easy.

It is quite easy and i think any programmer of minimum experience will be able to conclude it out.

So Okay here it goes.

  • Instructions :
  • Okay First You Need to take in 3 values from the User the DATE, MONTH AND YEAR
  • Next You will need to add in the number of days of the years to another variable "NUMBER" which starts with '0' As its initial Value.
  • Then From the year '0' to the Current 'YEAR'(Use While Loop) add in 365 for a normal year and 366 for a leap year. (You can do that with an if loop.)
    Note: Donot Add in the Current Year ... I mean use '<' But not '<='
  • After That Initialize 2 Arrays with normal year month-days and Leap year Month Days. You can do something like this and keep it global or in main()
    int leapmonths[12]={31,29,31,30,31,30,31,31,30,31,30,31};
    int avg_months[12]={31,28,31,30,31,30,31,31,30,31,30,31};
  • Now Add Each Value of the month until you reach your Current 'MONTH' (Use A While Loop) Note: Donot Add in the Current Month ... I mean use '<' But not '<='
  • After You Have Done that Add in the 'DATE'
  • Now Divide the Number by 7 and take its remainder.
  • Then Use a Group of Switch Statements to Tell you the Day.
    Note :I am not sure whether The Normal Calender Year 1-1-0001 was a Sunday So try entering the current date which is today and see if you get the correct Answer. Make Adjustments .

Hope This helps you out.

IF you have any errors or problems with the code. Post it down and we will try to fix it up ;)

Hi sash007,

there is a formula called Zeller's congurence to computing the day of week for are given date. You may look here: http://en.wikipedia.org/wiki/Zeller's_congruence

A programme in C++ that if date is input it tells day?
For example
Enter Date : 1-01-1985
The day is 'Monday'

Enter Date : 2-01-1985
The day is 'Tuesday'

Below program is a realisation of Zeller's congruence with the modification that 1 denotes Monday (now ISO standard). Btw, if 01 is month then 2-01-1985 is a Wednesday.

typedef unsigned long int ulint; 
// Day of week, Mon=1, Sun=7 (modified Zeller's congruence to meet ISO)
// usage: cout << "Day of week: " << DayOfWeek(1985, 1, 2) << endl;
// Day of week: 3
ulint DayOfWeek ( ulint year, ulint month, ulint day )
{ ulint j, m;
  if (month < 3) {m= month + 12; j= year - 1;} else {m= month; j= year;}
  return ((day + (13* m - 32)/ 5 + j + j/ 4 - j/ 100 + j/ 400)% 7)+ 1;}

krs,
tesu

hi again,

for the sake of completeness, the algorithm you have posted in #3 is exactly what is called Zeller's congruence.

krs,
tesu

AFTER 6 tiring days of heavy work and AFTER 5 wrong logics AT LAST done the challange withsimple if- else.

IT IS NOT COMPLETE JUST FOR 10, 11 and 12 month but can complete it.
GUESS WHAT? WHEN I WAS SWLEEPING I FOUND THIS LOGIC DREAMING:icon_cheesygrin: :icon_lol:
THANKS 4 THE HELP GUYS.
"A Coder lives his life thinking all time, even in Toilet"

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
long d,l,m,y1,r,fday,day2,y,day,ly,day3;
char v='/';
cout<<"\n\n\nEnter the date please in this format eg(25/06/2008) ";
cin>>d>>v>>m>>v>>y;
day=y-1900;
ly=day/4;
day2=(day*365)+ly;
if(m==12)
{if (d==1)
  fday=day2-30;
  if (d==2)
  fday=day2-29;
 if (d==3)
  fday=day2-28;
 if (d==4)
  fday=day2-27;
if (d==5)
  fday=day2-26;
if (d==6)
  fday=day2-25;
if (d==7)
  fday=day2-24;
if (d==8)
  fday=day2-23;
if (d==9)
  fday=day2-22;
if (d==10)
  fday=day2-21;
if (d==11)
  fday=day2-20;
if (d==12)
  fday=day2-19;
if (d==13)
  fday=day2-18;
if (d==14)
  fday=day2-17;
if (d==15)
  fday=day2-16;
if (d==16)
  fday=day2-15;
if (d==17)
  fday=day2-14;
if (d==18)
  fday=day2-13;
if (d==19)
  fday=day2-12;
if (d==20)
  fday=day2-11;
if (d==21)
  fday=day2-10;
if (d==22)
  fday=day2-9;
if (d==23)
  fday=day2-8;
if (d==24)
  fday=day2-7;
if (d==25)
  fday=day2-6;
if (d==26)
  fday=day2-5;
if (d==27)
  fday=day2-4;
  if (d==28)
  fday=day2-3;
if (d==29)
  fday=day2-2;
if (d==30)
  fday=day2-1;
if (d==31)
  fday=day2;
  }
  else
  if(m==11)
{if (d==1)
  fday=(day2-31)-29;
  if (d==2)
  fday=(day2-31)-28;if (d==3)
  fday=(day2-31)-27;if (d==4)
  fday=(day2-31)-26;if (d==5)
  fday=(day2-31)-25;if (d==6)
  fday=(day2-31)-24;if (d==7)
  fday=(day2-31)-23;if (d==8)
  fday=(day2-31)-22;if (d==9)
  fday=(day2-31)-21;if (d==10)
  fday=(day2-31)-20;if (d==11)
  fday=(day2-31)-19;if (d==12)
  fday=(day2-31)-18;if (d==13)
  fday=(day2-31)-17;if (d==14)
  fday=(day2-31)-16;if (d==15)
  fday=(day2-31)-15;if (d==16)
  fday=(day2-31)-14;if (d==17)
  fday=(day2-31)-13;if (d==18)
  fday=(day2-31)-12;if (d==19)
  fday=(day2-31)-11;if (d==20)
  fday=(day2-31)-10;if (d==21)
  fday=(day2-31)-9;if (d==22)
  fday=(day2-31)-8;if (d==23)
  fday=(day2-31)-7;if (d==24)
  fday=(day2-31)-6;if (d==25)
  fday=(day2-31)-5;if (d==26)
  fday=(day2-31)-4;if (d==27)
  fday=(day2-31)-3;if (d==28)
  fday=(day2-31)-2;if (d==29)
  fday=(day2-31)-1;if (d==30)
  fday=(day2-31);}
   else
  if(m==10)
{if (d==1)
  fday=(day2-61)-30;if (d==2)
  fday=(day2-61)-29;if (d==3)
  fday=(day2-61)-28;if (d==4)
  fday=(day2-61)-27;if (d==5)
  fday=(day2-61)-26;if (d==6)
  fday=(day2-61)-25;if (d==7)
  fday=(day2-61)-24;if (d==8)
  fday=(day2-61)-23;if (d==9)
  fday=(day2-61)-22;if (d==10)
  fday=(day2-61)-21;if (d==11)
  fday=(day2-61)-20;if (d==12)
  fday=(day2-61)-19;if (d==13)
  fday=(day2-61)-18;if (d==14)
  fday=(day2-61)-17;if (d==15)
  fday=(day2-61)-16;if (d==16)
  fday=(day2-61)-15;if (d==17)
  fday=(day2-61)-14;if (d==18)
  fday=(day2-61)-13;if (d==19)
  fday=(day2-61)-12;if (d==20)
  fday=(day2-61)-11;if (d==21)
  fday=(day2-61)-10;if (d==22)
  fday=(day2-61)-9;if (d==23)
  fday=(day2-61)-8;if (d==24)
  fday=(day2-61)-7;if (d==25)
  fday=(day2-61)-6;if (d==26)
  fday=(day2-61)-5;if (d==27)
  fday=(day2-61)-4;if (d==28)
  fday=(day2-61)-3;if (d==29)
  fday=(day2-61)-2;if (d==30)
  fday=(day2-61)-1;if (d==31)
  fday=(day2-61);}

r=fday%7;

if(r==0)
cout<<" THE DAY IS **** MONDAY **** ";
else
if(r==1)
cout<<" THE DAY IS **** TUESDAY **** ";
else
if(r==2)
cout<<" THE DAY IS **** WEDNESDAY **** ";
else
if(r==3)
cout<<" THE DAY IS **** THURSDAY **** ";
else
if(r==4)
cout<<" THE DAY IS **** FRIDAY **** ";
else
if(r==5)
cout<<" THE DAY IS **** SATURDAY **** ";
else
if(r==6)
cout<<" THE DAY IS **** SUNDAY **** ";

getch();
}

HUGE PROGRAMME

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.