











Maximum number of handshakes
Java program for finding Maximum number of handshakes
The given program will find the maximum number of handshakes in a room. Suppose there are N persons in a room. We have to find the maximum number of Handshakes possible. Given the fact that any two persons shake hand only once.
Example:
N=3
namely A,B,C
A shakes hand with B and C. now B shakes hand with C.Thus total number of ways to shake hands are 2+1 = 3 ways .


Explanation:
Suppose you entered into a room and there are N numbers of people .You shake hand with your friend so now you have N-1 people left to shake hands with . Similarly for your friend there are N-2 people left and so on.
Thus the above problem can be solved by simple nC2 .
Implementation
- Start
- user is asked to input an integer value n, representing the number of people
- calculate answer as n * (n – 1) / 2 as explained earlier .
Example:
n=3
3*(3-1)/2=3
thus 3 ways.
Print the output from the above program
- End
Code in Java:
// Java program to find maximum number of handshakes
import java.io.*;
import java.util.*;
class handshakes
{
// Calculating the maximum number of handshakes
static int maxHandshake(int n)
{
return (n * (n – 1)) / 2;
}
// Driver code
public static void main (String[] args)
{
Scanner sc=newScanner(System.in);
int n = sc.nextLine();
System.out.println( maxHandshake(n));
}
}
output:
20
190
Login/Signup to comment