# Thread: Problem with if statement

1. ## Problem with if statement

There is an example in my book that asks 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:

Code:
```#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 anyone explain it for me?

2. if i is evenly divisible by 5 then print a newline character.

3. % used in this way, is the modulus (aka "mod") operator. It ignores the normal answer you'd get from a division, and only keeps the remainder.

So 5 % 5 equals 0, since there is no remainder left over, after you divide 5/5.

mod operator is used a lot to handle things like columns, on the display. Here, it prints a newline (causing a carriage return and line feed), on the console, every fifth column.

Tip: 0 / 5 equals 0, so if you want 5 columns in the first row, and you have i starting with zero, (instead of the one in your book), you need to use:
Code:
```if(column % 5 == 0 && column)
printf("\n");```
The "&& column" part means that when column is zero, the expression will still evaluate as false, and a newline will not be printed.

4. Originally Posted by rags_to_riches
if i is evenly divisible by 5 then print a newline character.
% used in this way, is the modulus (aka "mod") operator. It ignores the normal answer you'd get from a division, and only keeps the remainder.

So 5 % 5 equals 0, since there is no remainder left over, after you divide 5/5.

mod operator is used a lot to handle things like columns, on the display. Here, it prints a newline (causing a carriage return and line feed), on the console, every fifth column.

Tip: 0 / 5 equals 0, so if you want 5 columns in the first row, and you have i starting with zero, (instead of the one in your book), you need to use:
Code:
```if(column % 5 == 0 && column)
printf("\n");```
The "&& column" part means that when column is zero, the expression will still evaluate as false, and a newline will not be printed.
Thanks a million.......things become easier when they are shared