Handshake

At the annual meeting of the Board of Directors of Acme Inc. If everyone attending shakes hands exactly one time with every other attendee, how many handshakes are there?
Find the number  of handshakes

Formula: nC2=n*(n-1)/2

Example

Input:  n=5
Output: 10

Approach:

Java

public class Handshake {
    public static void main(String[] args) {
        int n = 5;
        int handshake = handshake(n);
        System.out.println("Total Handshake " + handshake);
    }

    static int handshake(int n) {
        // formula for no of handshakes
        // is nC2=n*(n-1)/2
        return n * (n - 1) / 2;
    }

}

C++

#include <bits/stdc++.h>
using namespace std;
 int main()
{
  
   int n=5;

   //formula for no of handshakes 
   //is nC2=n*(n-1)/2
   int ans=(n-1)*n/2;
   cout<<ans<<"\n";

}

C

#include <stdio.h>

int handshake(int n)
{
    return n * (n - 1) / 2;
}

int main()
{

    int n = 5;

    printf("%d\n"handshake(n));

    return 0;
}


No comments:

Post a Comment