# Semester 1 Final Program: Coin Flip Probablity

## Code

///Name: Mark Katz
///Period: 6
///Program Name: Semester 1 Final
///File Name: sem1final.java
///Date Finished: 1/22/16
import java.util.Random;
import java.util.Scanner;
public class semi1final
{
public static void main ( String[] args )
{
Scanner keyboard = new Scanner(System.in);
Random r = new Random();
int flips; // I named this flips because it would be easy to keep track of, same with heads and tials.
int heads =0;
int tails =0;
int side=0;
System.out.println( "Semester 1 Final!" );
System.out.println( "");
System.out.println( "How many times would you like to flip the coin? ( 1-2,100,000,000)" );
System.out.print( ">" );
flips = keyboard.nextInt(); // I made flips and integer because I don't want a decimal.
while ( flips < 1 || flips > 2100000000 ); ; // I did a while loop because I can make sure the number is in between the correct two numbers, and it will check the number until it is right.
{
if ( flips < 1 )
{
System.out.println( "That number is too small. Please input a number between 1 and 2,100,000,000" );
flips = keyboard.nextInt();
}
else if ( flips > 2100000000)
{
System.out.println( "That number is too big. Please inpit a number between 1 and 2,100,000,000" );
}
}
for ( int n=0; n < flips; n++ ) // I used a for loop so that every time the program inside it runs it will count the number of heads or tails.
{
side = r.nextInt(2);
if ( side == 1 )
{
heads ++;
}
else
{
tails ++;
}
}
double probOfHeads = 100 * ( (double)heads / flips ); // I made the probabilities doubles because they were most likely not whole numbers and I wanted a precise answer.
double probOfTails = 100 * ((double)tails / flips );
System.out.println( "You flipped " + heads + " heads and " + tails + " tails." );
System.out.println( "The probability of you flipping heads is " + probOfHeads + "%." );
System.out.println( "The probability of you flipping tails is " + probOfTails + "%." );
}
}
// I found that using the largest number possible, 2.1 billion, would consistantly be close to 50% each.