Loop code problem

This is a discussion on Loop code problem within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to write a program that counts the even numbers from 1-100.I am trying to do this ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    11

    Loop code problem

    Hi, I am trying to write a program that counts the even numbers from 1-100.I am trying to do this by only using "while" loops.Here is my code at the moment:

    Code:
    #include<stdio.h>
    
    
    int main ()
    
    
    {
    
    
    int counter=0;
    
    
    while (counter<=100 && counter % 2==0){
    
    
    printf("%d\n",counter);
    
    
    counter=counter+1;
    
    
    }
    
    
    return 0;

    When i compile it it just prints a 0 then a space!

    any ideas?

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Because the loop breaks when it reaches 1.
    Put the if clause checking for even numbers within the loop body, not in the condition.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Because it's odd?

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Yes, and the loop will only run as long as it condition is valid.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I have had a very stressful day and finally this is working thanks!!

  6. #6
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Another way would be to use a for loop and increment by 2 each time. Won't have to mess with %.

    Code:
    for(i = 0; i < 101; i += 2)
    {
    counter += i
    }

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    I see what you mean but we havnt covered for loops yet so ill have to leave it!If i wanted a loop to return to the start of the program. I have encountered another problem in my next question regarding returning a loop to the start.

    Code:
    include <stdio.h>
    
    #define NUMBER 8
    
    
    int main ()
    
    
    {
    
    
    int guess;
    
    
    ("Please enter a guess between 1 and 10:");
    
    
    scanf("%d", guess);
    
    
    while (guess >=11)
    
    
    if (guess==NUMBER){
    
    
    printf("\nYou have guess correctly");
    
    
    }
    
    
    if (guess<NUMBER){
    
    
    printf("\nYou have guessed incorrectly, the");
    
    
    }
    
    
    if (guess>NUMBER){
    
    
    printf("\nYou have guessed incorrectly");
    
    
    }
    
    
    return 0;
    
    
    }
    I have not fully completed some of the printf statements but it should run!!
    Last edited by Laika1986; 10-19-2011 at 12:23 PM.

  8. #8
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    You forgot the closing bracket for your while loop.

  9. #9
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Now it's really not going to work. You need the brackets for the while loop. Also you dont need the guess<NUMBER or guess>NUMBER.

    Code:
    while()
    {
    
    if(number is ==) {
    print correct
    } else {
    print incorrect
    }
    }

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    So far so good but it's creating an infinite loop whereas i want to send it back to the start of the program!Thanks guys i appreciate it

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    33
    Why do you need it to go back to the start of the program? Can't you just add another scanf function call at the bottom of the while loop? (I'm assuming you reason for needing to return to the beginning would be to enter another guess).

  12. #12
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    Thanks Csaw, I did what you suggested and it has helped but now it wont recognise the right guess!

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Laika1986 View Post
    Hi, I am trying to write a program that counts the even numbers from 1-100.I am trying to do this by only using "while" loops.Here is my code at the moment:

    When i compile it it just prints a 0 then a space!

    any ideas?
    That's an easy one...
    Code:
    counter = 2;
    
    while (counter < 101)
      { printf("%d\t",counter);
        counter += 2; }
    If you want to know how many there are... count = limit / 2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code help; loop problem
    By Watts53 in forum C Programming
    Replies: 3
    Last Post: 08-04-2010, 05:39 PM
  2. loop problem in code
    By te5la in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2008, 12:44 PM
  3. this is my code which is not performing the for loop!
    By wajeeha.javed in forum C++ Programming
    Replies: 9
    Last Post: 12-10-2006, 11:12 AM
  4. snippet of for loop code problem
    By rayrayj52 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2004, 03:36 PM
  5. how to loop back for this code ?
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 12:40 PM

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