Variable In While Loop

This is a discussion on Variable In While Loop within the C Programming forums, part of the General Programming Boards category; I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At ...

  1. #1
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    18

    Variable In While Loop

    I'm writing a program to sum up the even numbers in between 1 and 100, using a while loop. At the beginning of my program, I initialize a variable "sum" to 0, and a variable "temp" to 1. Afterwards I enter a loop where I determine if "temp" is even or not, and if so add it to sum. However, at the end of my program, when I print "sum", I get a result of 0. Below is my code.

    Code:
    #include <stdio.h> 
    
    
    int main(void)
    {
       int sum = 0, temp = 1; 
       
       while(temp <= 100)
       {
                  if(temp % 2 == 0)
                          sum += temp; 
                  temp = temp + 1; 
       }
       
       printf("Sum is %i", sum); 
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,313
    I compiled and ran your program to get the output:
    Code:
    Sum is 2550
    It might help to write a blank line at the end, e.g.,
    Code:
    printf("Sum is %i\n", sum);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    18
    Nevermind, it works. I just ran the program and got the right output.

  4. #4
    Registered User Mcdom34's Avatar
    Join Date
    Jun 2012
    Location
    North Royalton, Ohio, United States
    Posts
    18
    How can I get rid of the extra blank lines when I post my code within tags?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,174
    You sure you're actually compiling that code? It should give a value of 2550. It does so on my system.

    Try deleting the executable, object files, and rebuild from scratch. In an IDE, do a "Build Clean", then "Build All" (noting those processes may have slightly different names, depending on IDE).

    Note that some IDE's will output an additional line reporting that the program exits with return value zero. With those IDE's you need to scroll up one line. It would help if the printf() statement appended a '\n' at the end (i.e. printf("Sum is %i\n", sum)) to avoid output being overwritten.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner, help with loop and variable
    By keeganstarr in forum C Programming
    Replies: 16
    Last Post: 05-28-2012, 07:31 PM
  2. looping a variable within a loop.
    By vespine in forum C Programming
    Replies: 3
    Last Post: 06-27-2010, 10:30 PM
  3. While loop with variable problem
    By Todd88 in forum C++ Programming
    Replies: 33
    Last Post: 04-06-2008, 07:36 PM
  4. Variable declaration with `for' loop
    By slippy in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2007, 03:14 PM
  5. Loop variable
    By anirban in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 10:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21