Thread: Problem with if statement

  1. #1
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16

    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?
    Last edited by Sharifhs; 08-11-2010 at 10:34 AM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    if i is evenly divisible by 5 then print a newline character.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    % 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. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Dhaka, Bangladesh
    Posts
    16
    Quote Originally Posted by rags_to_riches View Post
    if i is evenly divisible by 5 then print a newline character.
    Quote Originally Posted by Adak View Post
    % 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  3. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM
  4. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:06 PM

Tags for this Thread