Thread: validating a magic date

  1. #1
    Registered User
    Join Date
    Nov 2021

    Question validating a magic date

    H! I am making a program which takes date month and year as input and validates that whether the given date is a magic date or not the criteria for magic date is
    1)if month*date is one digit number and it is equal to last digit of the year it is a magic date for example 02,03,2006.
    2)if month*date is a two digit number and it is equal to last two digits of the year it is a magic date for example 09,02,2018
    3)if month*date is a three digit number and it is equal to last three digits of the year it is a magic date for example 30,10,2300.

    now programs requirements are that the input year should be of four digits and this program should be made by using switch statement.

    problem: i have made this program using if else but i dont understand how can i make it using switch statement.

    using namespace std;
    int main()
        int dd,mm,yyyy,a,b,c,d;
        cout<<"enter date ";
            cout<<"invalid date";
        cout<<"enter month ";
            cout<<"invalid month";
        cout<<"enter year ";
            cout<<"invlaid year range ";
            cout<<"special date"<<endl;
        else if(yyyy%100==(mm*dd))
            cout<<"special date"<<endl;
        else if(yyyy%1000==(mm*dd))
            cout<<"special date"<<endl;
            cout<<"not a special date"<<endl;
        return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    switch / case work well when
    - one side of your if expressions are constants
    - those constants are close together.

    But your if's are variable on both sides, and the range is 1 to 372 (31*12)

    I suppose you could contrive something with
    int magic = mm * dd;
    string s = std::to_string(magic);
    switch ( s.length() ) {
      case 1:
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program that output the very next date after Input a date
    By rumel.ehsan in forum C Programming
    Replies: 7
    Last Post: 03-23-2013, 11:53 AM
  2. Replies: 2
    Last Post: 05-07-2012, 07:37 AM
  3. Replies: 11
    Last Post: 03-27-2012, 11:37 PM
  4. validating a date month and year format
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 08-16-2005, 07:45 AM
  5. the magic of MAGIC numbers
    By borko_b in forum A Brief History of
    Replies: 8
    Last Post: 06-27-2002, 02:36 AM

Tags for this Thread