tags:

views:

149

answers:

9

There was an example in my book where it was asked to write a program that prints the number 1 to 100 using 5 columns (Have each number separated from the next by a tab). The solution was as following:

#include "stdio.h"
int main()
{
 int i;

 for(i=1; i<=100; i++) {
  printf("%d\t", i);
  if((i%5)==0) printf("\n");
 }
 return 0;
}

But I can't understand the if((i%5)==0) printf("\n"); statement. Could you explain it for me?

+10  A: 

The % operator is the modulus operator (integer division's remainder). So every five loop iterations, your program will output a \n character (new line).

Values will be:

Iteration         i%5 value
      i=1                 1
      i=2                 2
      i=3                 3
      i=4                 4
      i=5                 0
      i=6                 1
      i=7                 2
      i=8                 3
      i=9                 4
     i=10                 0

So, every five prints, a \n (new line) will be printed to the standard output.

Hope it helps.

Pablo Santa Cruz
In other words, the statement `x % y == 0` will be true when `x` is evenly divisible by `y` (that is, `x` is a multiple of `y`). The technique of using `% y == 0` with a counter is an easy way of making something happen every `y` number of times through the loop.
bta
s/reminder/remainder/g
Nathon
@Nathon: thanks! Fixed that.@bta: true.
Pablo Santa Cruz
A: 

That means if i is divided by 5 with no remainder new line character is printed.

Michael
A: 

% is the modulo operator, which is the remainder after division. To expand it and make it easier to read,

if( (i%5) == 0 )
{
    printf("\n");
}

So, if the remainder of i divided by 5 is 0 (if i is evenly divisible by 5), print a newline. So, this will happen at i = 5, 10, 15, 20, etc.

Alex Zylman
+1  A: 

The if condition checks if the number represented by i is divisible by 5 .

5 % 5 = 0 // remainder 
5 / 5 = 1  // quotient
Praveen S
A: 

The i%5 returns the modulo (the remainder after division) of 5. So

1%5 = 1
2%5 = 2
3%5 = 3
4%5 = 4
5%5 = 0
6%5 = 1
etc...

Every 5th time through the loop the modulo is zero and a newline is printed.

Starkey
A: 
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
.........

The % operator is called modulus, it returns the remainder of the first argument divided by the second argument. Using it on the loop variable is a simple way to count every 5 iterations.

James
A: 

Your program prints the numbers 1 to 100, and when the number is divisible by 5, it goes to a new line. This way, it creates five columns (with 1, 2, 3, 4, 5 on top of the columns).

murgatroid99
A: 

Here's a link that shows more about the modulus operator.

Realize it's used as a bit of a "trick" sometimes like above, although complelety valid and it's used "everywhere" like that.

By trick, I mean it's used to print something every 5 lines. :-)

JustBoo
Good Grief, in the time it took to type out my anwser, 6 new answers arrived.
JustBoo
A: 

(i % 5) will be zero for every fifth time and '\n' will start printing from next line. 1'\t'2'\t'3'\t'4'\t'5'\n' 6.......

chaitanyavarma