Write a program to calculate Maximum number of handshakes

22 November 2022

Write a program to calculate Maximum number of handshakes



Description

Get the number of people in the room as input from the user. Then calculate the maximum number of handshakes possible among that people.

For e.g.

If there are N people in the room then the first person has to shake hand with N-1 people and second person has to shake hand with N-1-1 people i.e., N-2 handshakes are possible. Thus, it goes on.

So total hand shakes = N-1 + N-2 + N-3 +………+1 + 0

Input

10

Output

45


C Program

#include <stdio.h>

int main()

{

    int num;

    printf("Enter the number of people in the room: ");

    scanf("%d",&num);

    int total = num * (num-1) / 2;

    printf("Total handshakes = %d",total);

    return 0;

}


C++ Program

#include <iostream>

using namespace std;

int main()

{

    int num;

    cout<<"Enter the number of people in the room: ";

    cin>>num;

    int total = num * (num-1) / 2;

    cout<<"Total handshakes = "<<total;

    return 0;

}


Java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);

System.out.print("Enter the number of people in the room: ");

int num = sc.nextInt();

int total = num * (num-1) / 2;

System.out.println("Total number of handshakes = "+total);

}

}


Python

num= int(input("Enter the number of people in the room: "))

total = int(num * ((num - 1) / 2))

print("Total number of handshakes = ",total)


Related Articles

Ask Us Anything !