Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Coin Loop Selective Print Statement Problem

1. ## Coin Loop Selective Print Statement Problem

This is a homework assignment and I am stuck and I have researched every book I know that talks about loops.
I am required to print the loop at every 25th loop up to 500, i.e. 1-25th, 26-75th, 100th, 125th, etc.

I guess another way of stating my problem is that I want to sum up the toss results for every 25 loop iterations up to 500. Can someone please explain how I can do this? Right now my code is printing all 500 loops.

``` package coin;

import java.util.Random;

public class Coin
{
public static void main(String[] args)
{
int heads =0, tails =0, tossResult, total;
double tailsPercent, headsPercent;
Random coinTossing = new Random();

System.out.println("Number of tosses\t\tNo. of Heads\\ % \t\tNo. of Tails\\ %");

for (int i = 1; i <= 50; i++)
{
tossResult = coinTossing.nextInt(2);

if (tossResult == 0)

{
heads++;
}

else

{
tails++;
}

total = heads + tails;
headsPercent = ((double)(heads)/total)*100;
tailsPercent = ((double)(tails)/total)*100;

System.out.printf("\t%d\t\t\t\t%d  %.1f\t\t%d  %.1f", total, heads,
headsPercent, tails, tailsPercent);
System.out.println();
}

}
}```

If you know a good link that explains this too, please send it my way. Thank you!

2. ## Re: Coin Loop Selective Print Statement Problem

Your loop counter is i. You want to do something special for i = 25, i = 50, i = 75, ..., Every value of i that is divisible by 25, ...

In Java, you can find out whether an integer is divisible by another integer by using the "remainder" operator (the '%' sign), which divides the first operand by the second and returns the remainder as its result.

That is to say, for integers a and b:
a is divisible by b if (and only if) a%b is equal to zero.

Cheers!

Z

3. ## The Following User Says Thank You to Zaphod_b For This Useful Post:

sternfox (January 30th, 2013)