Thread: Simple C Program [for loops?]

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    Simple C Program [for loops?]

    Code:
    #include <stdlib.h>
    
    int main()
    {
    
         int i,total;
    
         for (i = 2; i<=31;i++)
         {
             if ( i%2 == 0) {
                printf("Number : %d \n",i);
                total += i;
                };
         };
    
         printf("Total : %d \n",total);
    
         system("PAUSE");
          return 0;
    }
    This is the way i coded my application however is it possible to do it without an if statement? by incrementing step 2? i tried

    for (i = 2; i<=31;i+2) but it just goes into an infinite loop displaying 2... what is wrong?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. total is not initialized
    2. i+2 does not change the value of i, use i += 2
    3. system("PAUSE"); - read forum in the last two days I saw a couple of advices agains usage of this function, I'm too lazy to repeate them.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    thanks man

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    just for the sake of perfection

    Code:
    #include<stdio.h>
    should be included

    ssharish2005

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    true

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    2
    Initializing it when declaring it is a good habit
    e.g:

    int i = 0;
    int total = 0;

    And better one per line, because

    int * a,b;

    It means "int * a; int b;" instead of " int * a;int * b;"

    And I like this form:
    Code:
    if ( i%2 == 0) 
    {
        printf("Number : %d \n",i);
        total += i;
    };
    ^_~

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't really need semicolons after all of your if statements and for loops . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  3. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM