Number of handshakes such that a person shakes hands only once
There are N number of persons in a party, find the total number of handshake such that a person can handshake only once.
Examples:
Input : 5 Output : 10 Input : 9 Output : 36
We can see a recursive nature in the problem.
// n-th person has (n-1) choices and after // n-th person chooses a person, problem // recurs for n-1. handshake(n) = (n-1) + handshake(n-1) // Base case handshake(0) = 0

Below is implementation of above recursive formula.
C++
// Recursive CPP program to count total // number of handshakes when a person // can shake hand with only one. #include <stdio.h> // function to find all possible handshakes int handshake(int n) { // when n becomes 0 that means all the // persons have done handshake with other if (n == 0) return 0; else return (n - 1) + handshake(n - 1); } int main() { int n = 9; printf("%d", handshake(n)); return 0; } |
chevron_right
filter_none
Java
// Recursive Java program to // count total number of // handshakes when a person // can shake hand with only one. import java.io.*; class GFG { // function to find all // possible handshakes static int handshake(int n) { // when n becomes 0 that // means all the persons // have done handshake // with other if (n == 0) return 0; else return (n - 1) + handshake(n - 1); } // Driver Code public static void main (String[] args) { int n = 9; System.out.print(handshake(n)); } } // This code is contributed // by chandan_jnu |
chevron_right
filter_none
Python3
# Recursive Python program # to count total number of # handshakes when a person # can shake hand with only one. # function to find all # possible handshakes def handshake(n): # when n becomes 0 that means # all the persons have done # handshake with other if (n == 0): return 0 else: return (n - 1) + handshake(n - 1) # Driver Code n = 9print(handshake(n)) # This code is contributed # by Shivi_Aggarwal |
chevron_right
filter_none
C#
// Recursive C# program to // count total number of // handshakes when a person // can shake hand with only one. using System; class GFG { // function to find all // possible handshakes static int handshake(int n) { // when n becomes 0 that // means all the persons // have done handshake // with other if (n == 0) return 0; else return (n - 1) + handshake(n - 1); } // Driver Code public static void Main (String []args) { int n = 9; Console.WriteLine(handshake(n)); } } // This code is contributed // by Arnab Kundu |
chevron_right
filter_none
PHP
<?php // Recursive PHP program to // count total number of // handshakes when a person // can shake hand with only one. // function to find all // possible handshakes function handshake($n) { // when n becomes 0 that means // all the persons have done // handshake with other if ($n == 0) return 0; else return ($n - 1) + handshake($n - 1); } // Driver Code $n = 9; echo(handshake($n)); // This code is contributed // by Shivi_Aggarwal ?> |
chevron_right
filter_none
Output:
36
We can come up with a direct formula by expanding the recursion.
handshake(n) = (n-1) + handshake(n-1)
= (n-1) + (n-2) + handshake(n-2)
= (n-1) + (n-2) + .... 1 + 0
= n * (n - 1)/2
C++
// Recursive CPP program to count total // number of handshakes when a person // can shake hand with only one. #include <stdio.h> // function to find all possible handshakes int handshake(int n) { return n * (n - 1)/2; } int main() { int n = 9; printf("%d", handshake(n)); return 0; } |
chevron_right
filter_none
Java
// Recursive Java program to // count total number of // handshakes when a person // can shake hand with only one. class GFG { // function to find all // possible handshakes static int handshake(int n) { return n * (n - 1) / 2; } // Driver code public static void main(String args[]) { int n = 9; System.out.println(handshake(n)); } } // This code is contributed // by Arnab Kundu |
chevron_right
filter_none
Python3
# Recursive Python program # to count total number of # handshakes when a person # can shake hand with only one. # function to find all # possible handshakes def handshake(n): return int(n * (n - 1) / 2) # Driver Code n = 9print(handshake(n)) # This code is contributed # by Shivi_Aggarwal |
chevron_right
filter_none
C#
// Recursive C# program to // count total number of // handshakes when a person // can shake hand with only one. using System; class GFG { // function to find all // possible handshakes static int handshake(int n) { return n * (n - 1) / 2; } // Driver code static public void Main () { int n = 9; Console.WriteLine(handshake(n)); } } // This code is contributed by Sachin |
chevron_right
filter_none
PHP
<?php // Recursive PHP program to // count total number of // handshakes when a person // can shake hand with only one. // function to find all // possible handshakes function handshake($n) { return $n * ($n - 1) / 2; } // Driver Code $n = 9; echo(handshake($n)); // This code is contributed // by Shivi_Aggarwal ?> |
chevron_right
filter_none
Output:
36
Recommended Posts:
- Maximum number of 3-person teams formed from two groups
- Find a time for which angle between hour and minute hands is given theta
- Counting pairs when a person can form pair with at most one
- Position of a person diametrically opposite on a circle
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Number of ways to divide a given number as a set of integers in decreasing order
- Total number of possible Binary Search Trees using Catalan Number
- Given number of matches played, find number of teams in tournament
- Find count of digits in a number that divide the number
- Count Number of animals in a zoo from given number of head and legs
- Find the number of ways to divide number into four parts such that a = c and b = d
- Check if a number has same number of set and unset bits
- Round-off a number to a given number of significant digits
- Immediate smallest number after re-arranging the digits of a given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



