Simple for loop question

This is a discussion on Simple for loop question within the C Programming forums, part of the General Programming Boards category; I need to create a program using a for loop that will read in an integer, and print all the ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Simple for loop question

    I need to create a program using a for loop that will read in an integer, and print all the multiples of that integer up to 1000. e.g. given 7, will print 7, 14, 21, 28...(until the end)

    This is what I have:
    Code:
    #include <stdio.h>
    
    int main() 
    {
        int a, b;
        
        printf("Enter in an integer:\n");
        scanf("%d", &a);
        
        for (a = 1; a < 1000;  b = a + a)
                printf("Your numbers are: %d.\n", b);
        
        return 0;
    }
    It seems to just be running an infinate loop.

    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Look at your for loop. You initialize a to 1, tell it to stop once a is greater than or equal to 1000 and increment b. Your loop variable should be b:
    Code:
    for (b = ?; b < 1000; b += ? )
    Now, what value does b start at in your example? Where did that come from? How much does b increment by each time?

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by anduril462 View Post
    Look at your for loop. You initialize a to 1, tell it to stop once a is greater than or equal to 1000 and increment b. Your loop variable should be b:
    Code:
    for (b = ?; b < 1000; b += ? )
    Now, what value does b start at in your example? Where did that come from? How much does b increment by each time?
    Thanks for the help

    Code:
    for (b = a; b < 1000;  b += a)
    Works like a charm!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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