# Final Exam 1

## Code

```      /// Name: Alex Pinder
/// Period: 5
/// Program Name: Final Semester 1
/// File Name: CoinFlip.java
/// Date Complete: 1/20/16

import java.util.Random;
import java.util.Scanner;

public class CoinFlip
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();

int times;

do
{
System.out.print( "How many times will you flip the coin? " );
times = keyboard.nextInt();

if ( times <= 0 )
System.out.println( "Choose a valid number" );
if ( times > 2100000000 )
System.out.println( "Choose a valid number" );
} while ( times <= 0 || times > 2100000000 )

int flip, counter;
double timesHeads, timesTails;

timesHeads = 0;
timesTails = 0;
counter = 0;

while ( counter != times )// This doesn't actually have to be a do-while loop so i didn't use one.
{
flip = 1 + r.nextInt(2);

if ( flip == 1 )
timesHeads++;

else if ( flip == 2 )
timesTails++;

counter++;

}

double probHeads = timesHeads / times;
double probTails = timesTails / times;

System.out.println( "\nYou flipped heads " + timesHeads + "  times and tails " + timesTails + " times.\n\nYou flipped heads " + (probHeads * 100) + "% of the time and tails " + (probTails * 100) + "% of the time." );
}
} // The number of flips you need to use to consistantly get as close to 50/50 is 2,100,000,000 because the more flips you loose the less likely it is to favor one side.
```