Monday, September 26, 2011

Coin Toss result

//This Java program accepts a number from the user
//and then performs that many coin tosses and prints
//the result of each toss and finally prints
//the total number of heads and tails.
import java.util.*;
class toss
{
public static void main(String Args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of tosses you want");
int n=sc.nextInt();
int head=0,tail=0,i;
int result;
for(i=1;i<=n;i++)
{
result=(int)(Math.random()*2);
if(result==0)
{
System.out.println("Heads");
head++;
}
else if(result==1)
{
System.out.println("Tails");
tail++;
}
}
System.out.println("Final Result :-");
System.out.println("Total Heads = "+head+" Total Tails = "+tail);
}
}
//Author : Mayank Rajoria

No comments:

Post a Comment