C++ Program to find the maximum number of handshakes

Program to find the maximum number of handshakes

Here we will discuss how to find the maximum number of handshakes which can happen between N number of people given the fact that any two people shake hands exactly once using C++ programming language.

For the number of handshakes to be maximum, every person should shakehand with every other person in the room i.e all persons present should shake hands. For the first person, there would be N-1 people to shake hands. For second person there would N-1 people available but as he had already shaken hands with the first person,  there would be N-2 people to shake hands and so on.

Therefore the total number of handshake = (N-1 + N-2 +….+ 1 + 0)=((N-1)*N)/2.

Maximum number of handshakes
Competitive Coding Techniques

Algorithm:-

  1. User gives an input(number of people).
  2. The input is stored in an int type variable say p.
  3. By using the formula the maximum number of handshakes between p people are found and stored in another variable say max.

            int max=p*(p-1)/2;

      4. max is printed as output.

Maximum number of handshakes in C++

C++ Code:-

    //C++ Program

//Maximum number of handshakes
   
#include<iostream>

    using namespace std;

    //main program

    int main()

    {

        int p;

        cout<<"Enter number of Persons: ";

        //user input

        cin>>p;

        cout<<"Maximum number of handshakes: ";

        //find maximum number of handshakes using formula

        int max=p*(p-1)/2;

        //printing output

        cout<<max;

        return 0;

    }

Output

    Enter number of Persons: 15
 Maximum number of handshakes: 105
coding (3)
  • Highest Common Factor(HCF): C | C++Java | Python
  • Lowest Common Multiple (LCM) : C | C++ | Java | Python
  • Greatest Common Divisor : C | C++ | Java | Python
  • Binary to Decimal to conversion : C | C++ | Java | Python
  • Binary to Octal conversion : C | C++ | Java  | Python
  • Decimal to Binary conversion: C | C++ | Java | Python
  • Decimal to octal Conversion: C | C++ | Java | Python
  • Octal to Binary conversion : C | C++ | Java | Python
  • Octal to Decimal conversion : C | C++ | Java | Python
  • Quadrants in which a given coordinate lies : C | C++ | Java | Python
  • Permutations in which n people can occupy r seats in a classroom : C | C++ | Java | Python
  • Maximum number of handshakes: C | C++ | Java | Python
  • Addition of two fractions: C | C++ | Java | Python
  • Replace all 0’s with 1 in a given integer : C | C++ | Java | Python
  • Can a number be expressed as a sum of two prime numbers : C | C++ | Java | Python
  • Count possible decoding of a given digit sequence : C | C++ | Java
  • Check whether a character is a vowel or consonant : C | C++ | Java | Python
  • Check whether a character is a alphabet or not : C | C++ | Java | Python
  • Calculate the area of a circle : C | C++ | Java | Python
  • Find the ASCII value of a character : C | C++ | Java | Python
  • Find the prime numbers between 1 to 100 : C | C++ | Java | Python
  • Calculate the number of digits in an integer : C | C++ | Java | Python
  • Convert digit/number to words : C | C++ | Java | Python
  • Counting number of days in a given month of a year: C | C++ | Java | Python
  • Finding Number of times x digit occurs in a given input : C | C++ | Java | Python
  • Finding number of integers which has exactly x divisors: C | C++ | Java | Python
  • Finding Roots of a quadratic equation : C | C++ | Java | Python