In: Computer Science
The sum of positive integers from 1 to 1 is 1;
The sum of positive integers from 1 to 2 is 3;
The sum of positive integers from 1 to 3 is 6;
The sum of positive integers from 1 to 4 is 10;
The sum of positive integers from 1 to 5 is 15;
⁞
The sum of positive integers from 1 to 100 is 5050.
(The program should include a main
method, and no tester class is needed)
2. You need to control the number of people who can be in an oyster
bar at the same time. Groups of people can always leave the bar,
but a group cannot enter the bar if they would make the number of
people in the bar exceed the maximum of 100 occupants. Write a
program that reads the sizes of the groups that arrive or depart.
Use negative numbers for departures. After each input, display the
current number of occupants. As soon as the bar holds the maximum
number of people, report that the bar is full and exit the
program.
1- Sum of first n natural numbers in (n*(n+1))/2. We will use this formula. Have a look at the below code.
2- This second solution quite intutive to understand. Just for sake of simplicity I have used constant integer for groups who are exiting. We will enter an infinite loop till the bar is not full and will break the loop when the bar capacity becomes greater than 100.
Happy
Learning!