previous date – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Fri, 26 Aug 2022 12:54:55 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg previous date – Programmerbay https://programmerbay.com 32 32 C program to Find Previous Date https://programmerbay.com/c-program-to-find-previous-date/ https://programmerbay.com/c-program-to-find-previous-date/#respond Thu, 21 Jul 2022 16:19:49 +0000 https://programmerbay.com/?p=5309 The program prints previous date of the entered input. It validates leap year, month and date to check whether the given date is valid or not. We have simply used nested if in the logic to achieve the functionality.

C Program to determine the previous date

Program:

#include<conio.h>
#include<stdio.h>

int
main ()
{
  int month, date, year, valid = -1, leap_year = -1, invalid_input = -1;
  printf ("please enter a date = ");
  scanf ("%d", &date);
  printf ("please enter a month = ");
  scanf ("%d", &month);
  printf ("please enter a year = ");
  scanf ("%d", &year);
  if ((date > 0 && date <= 31) && (month >= 1 && month <= 12)
      && (year >= 2000 && year <= 2050))
    {
      invalid_input = 1;
      // finding given input is a leap year or not
      if ((year % 4) == 0)
  {
    leap_year = 1;
    if ((year % 100) == 0)
      {
        if ((year % 400) == 0)
    {
      leap_year = 1;
    }
        else
    {
      leap_year = -1;
    }
      }
  }
      if (month == 2 && leap_year == 1 && date > 29)
  valid = -1;
      else if (month == 2 && leap_year == -1 && date > 28)
  valid = -1;
      else
  valid = 1;
    }

  if ((month == 6 || month == 4 || month == 9 || month == 11) && date > 30)
    valid = -1;

// validating & finding output


  if (valid == 1)
    {
      printf ("Entered date = %d-%d-%d", date, month, year);

      if (date == 1)
  {
    if (month == 1)
      {
        date = 31;
        month = 12;
        year--;
      }
    else if (leap_year = 1 && month == 3)
      {
        date = 29;
        month--;
      }
    else if (leap_year == -1 && month == 3)
      {
        date = 28;
        month--;
      }
    else if (month == 2 || month == 4 || month == 6 || month == 8
       || month == 9 || month == 11)
      {
        date = 31;
        month--;
      }
    else
      {
        date = 30;
        month--;
      }
  }
      else
  {

    date--;
  }
      printf ("\nPrevious date = %d-%d-%d \n", date, month, year);
    }

  if (valid == -1 && invalid_input == 1)
    printf ("\n Not a valid date");

  if (invalid_input == -1)
    printf ("\n Invalid Input");

  return 0;
}

 

Output:

Testing the program:

Invalid Date:

please enter a date = 31
please enter a month = 6
please enter a year = 2030

 Not a valid date

 

Previous Date:

please enter a date = 21
please enter a month = 6
please enter a year = 2030
Entered date = 21-6-2030
Previous date = 20-6-2030 

]]>
https://programmerbay.com/c-program-to-find-previous-date/feed/ 0
Design the test cases and test the program of previous date problem by using Equivalence Class testing https://programmerbay.com/design-the-test-cases-and-test-the-program-of-previous-date-problem-by-using-equivalence-class-testing/ https://programmerbay.com/design-the-test-cases-and-test-the-program-of-previous-date-problem-by-using-equivalence-class-testing/#respond Sat, 21 Sep 2019 16:52:47 +0000 https://programmerbay.com/?p=5284 In this article we’ll be creating test cases for previous date problem using Equivalence Class testing. In this technique, we split input domain into two groups or partitions, valid and invalid.

You can check out the “previous date problem” program along with its tested test cases here.

First let’s identify the output domain for the previous date problem.

O1 = Previous date
O2 = Invalid date

Coming to Input domain,

I1 = {1 <= date <=31 }
I2 = {date<0 }
I3 = {date>31}
I4 = {1<= month <=12 }
I5 = {month<1 }
I6 = {month >31}
I7 = {2000<= year <=2050}
I8 = {year<2000 }
I9 = {year>2050}

In this problem, we can clearly see 2 test cases for output and 9 test cases for input are made.

Test casesDateMonthYearExpected OutputProgram OutputTest Outcome
O1156202514-6-202514-6-2025Pass
O23162025Invalid DateInvalid DatePass
I1156202514-6-202514-6-2025Pass
I2062025Invalid InputInvalid InputPass
I33262025Invalid InputInvalid InputPass
I4156202514-6-202514-6-2025Pass
I515-12025Invalid InputInvalid InputPass
I615132025Invalid InputInvalid InputPass
I7156202514-6-202514-6-2025Pass
I81561999Invalid InputInvalid InputPass
I91562051Invalid InputInvalid InputPass

]]>
https://programmerbay.com/design-the-test-cases-and-test-the-program-of-previous-date-problem-by-using-equivalence-class-testing/feed/ 0