output – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Fri, 26 Aug 2022 16:18:14 +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 output – Programmerbay https://programmerbay.com 32 32 C Program To Check whether Triangle is Equilateral, Isosceles or Scalene https://programmerbay.com/c-program-to-classify-a-triangle/ https://programmerbay.com/c-program-to-classify-a-triangle/#respond Sun, 21 Aug 2022 16:33:46 +0000 https://programmerbay.com/?p=5308 The program determines the type of triangle based on the input provided by a user.

A triangle can be broadly classified according to the length of its sides and the angle between the sides.
Based on the sides, there are three types of triangle
1. Equilateral Triangle
2. Isosceles Triangle
3. Scalene Triangle

Based on the angles, there are also 3 types:
1. Acute triangle
2. Right triangle
3. Obtuse triangle

In this article, we’ll be discussing triangles that are classified based on sides, and implementing the same in the program.

type of triangle

  1. Equilateral Triangle: A triangle in which all sides are of the same length
  2. Isosceles Triangle: A triangle in which exactly two sides are equal
  3. Scalene Triangle: A triangle in which all sides are having different length

Approach to determine the type of a triangle

  1. Read the length of a triangle for the user
  2. If side1, side2 and side3 are equal to each other, then
    • print a message the triangle is an equilateral triangle
  3.  If any two from side1, side2 or side3 are having the same length, then
    • print message the triangle is isosceles triangle
  4.  If all three sides are different in length, then
    • print message the triangle is a scalene triangle

C Program To Check whether Triangle is Equilateral, Isosceles or Scalene

The program also implies Triangle Inequality Theorem to validate whether the given sides can be a triangle or not.

Program:

#include<conio.h>
#include<stdio.h>
#include<math.h>
void
main ()
{
  int a, b, c, flag = -1;
  printf (" Enter the values of a, b and c : = ");
  scanf ("%d %d %d", &a, &b, &c);
  if ((a >= 0 && a <= 10) && (b >= 0 && b <= 10) && (c >= 0 && c <= 10)){
 
 // Triangle Inequality Theorem : every side's length should be shorter than sum of other two side
      if (((a + b) > c) && ((b + c) > a) && ((c + a) > b)){
    flag = 1;
    if ((a == b) && (b == c))
      printf ("\n It is an Equilatral Triangle");
    else if ((a == b) || (b == c) || (c == a))
      printf ("\n It is an isosceles Triangle");
    else
      printf ("\n It is a Scalene Triangle");
      }
    }
    
  if (flag == -1)
    printf ("Given side inputs, can't be a triangle ");
    
  getch ();
}

 

Testing Result of the Program

Not a Triangle

 Enter the values of a, b and c : = 11 5 5
Given side inputs, can't be a triangle 

Equilateral triangle

Enter the values of a, b and c : = 4 4 4

It is an Equilateral Triangle

Isosceles Triangle

Enter the values of a, b and c : = 2 3 2

It is an isosceles Triangle

 

]]>
https://programmerbay.com/c-program-to-classify-a-triangle/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

 

]]>