code – 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 code – 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
Difference between wait and sleep in Tabular form https://programmerbay.com/difference-between-wait-and-sleep/ Thu, 27 Feb 2020 14:02:23 +0000 https://programmerbay.com/?p=6148 In Multithreading, both wait() and sleep() methods are used to make a thread to sleep. The main difference between both of them is, wait() puts a thread to sleep and forces it to move to suspended state, whereas, sleep puts a thread sleep for specified milliseconds.

Difference between wait() and sleep() in Tabular form

Sleep MethodWait Method
It puts a thread to sleep for some time and makes
that thread to automatically wake up after
specified time ends
It forces a thread to sleep until notify() gets triggered
It doesn't force a thread to release its lock before completing its taskIt forces a thread to release its lock before completing its task
It is not mandatory to define it within synchronised blockIt is mandatory to define it within synchronised block
It halts a thread execution temporarilyIt halts a thread execution permanently
It is invoked on threadIt is invoked on object
A thread awakes automatically or by interrupt()A thread awakes by notify() and notifyAll()
It controls a thread's execution timeIt controls multi-thread synchronization

wait() Method

Wait method makes a thread sleep and forces it to move to suspended state.

It also causes a thread to release its acquired lock.So that, other threads can get turn in order to perform their respective jobs.
A wait method always pair with a notify method. Both together provides a way of interthread communication.

Java program to demonstrate working of wait method ?

Program:

class WaitExample extends Thread
{
 @Override
 public void run() {
 print();
 }
 synchronized void print()
 {
 System.out.println("THis is child ");
 notify();
 }
}
public class Main {
 public static void main(String[] args) {
 WaitExample thread = new WaitExample();
 thread.start();
 synchronized (thread)
 {
 System.out.println("I am in synchronised :");
 try {
 thread.wait();
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 System.out.println("I am in invoked :");
 }

Output:

anotherone

sleep() Method

Sleep method puts a thread to sleep too but for specified  milliseconds.

It allows a thread to carry out its  remaining task once provided time ends. Unlike wait(), it doesn’t order a thread to release its locks until it completes its task.

Java program to demonstrate working of sleep method ?

Program:

class ThreadExample extends Thread{
    @Override
    public void run() {
        System.out.println("Child thread is about to sleep");
        try {
            sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("I am child Thread ");
    }
}
public class Main {

    public static void main(String[] args) {
ThreadExample thread1 = new ThreadExample();
thread1.start();
        System.out.println("I am Main Thread ");
    }
}

Output

Child thread is about to sleep

I am Main Thread
I am child Thread

 

]]>
Wap to convert degree to radian? https://programmerbay.com/wap-to-convert-degree-to-radian/ https://programmerbay.com/wap-to-convert-degree-to-radian/#respond Wed, 25 Sep 2019 06:05:22 +0000 https://programmerbay.com/?p=5343 Program:

#include<stdio.h>
int main()
{
int degree;
printf("Enter degrees = ");
scanf("%d",&degree);
printf("\n %d degrees are equivalent to %.2f radians\n\n",degree,(float)(3.14*degree)/180);
return 0;
}

Output:

degree to radian

 

]]>
https://programmerbay.com/wap-to-convert-degree-to-radian/feed/ 0