Creating program using for loops

This is a discussion on Creating program using for loops within the C Programming forums, part of the General Programming Boards category; I need to create a program that gives this output 0000000000 999999999 88888888 7777777 666666 55555 4444 333 22 1 ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    13

    Exclamation Creating program using for loops

    I need to create a program that gives this output

    0000000000
    999999999
    88888888
    7777777
    666666
    55555
    4444
    333
    22
    1

    I need help on how to go about with this. THanks

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Any help is appreciated...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    What is your idea and what have you tried?
    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

  4. #4
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    I honestly don't know where to start or I wouldn't be asking this question

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Okay. Let's change the question to:
    create a program that gives this output

    0000000000

    Now, what idea do you have to write the program? Refer to what you have learned.
    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

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    In order to do that all i would need is a single printf statement

    printf("0000000000");

    How exactly does this help me

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Good. That's a valid solution. However, you also wrote in the title to this thread "using for loops". Incorporate that requirement into your solution to this simplified problem in a reasonable way.
    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

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    Could i start my program off with just that printf statement and then use for loops for the rest of the pyramid?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Quote Originally Posted by g3tb0mbed
    Could i start my program off with just that printf statement and then use for loops for the rest of the pyramid?
    You certainly could do that if you wanted to. However, that misses the point as you could just have a single printf statement:
    Code:
    printf("0000000000\n"
           "999999999\n"
           "88888888\n"
           "7777777\n"
           "666666\n"
           "55555\n"
           "4444\n"
           "333\n"
           "22\n"
           "1\n");
    If that is an acceptable solution, go for it. But you mentioned "for loops", so the point of my simplified exercise is to get you started on a for loop. If you want to do a printf then use for loops, go ahead, but then if you still ask how to do "the rest of the pyramid", I'll just present you with another simplified problem that amounts to the same thing.
    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

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    13
    No that definitely wont be acceptable. Sorry its my fault im really new to this and missed the first two weeks of class due to being in the hospital. Ill go through the textbook again and see if i can learn anything. Thanks for the help

  11. #11
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    What's the difference between a for loop and a while loop?

  12. #12
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    im trying this question topic
    and i get a little question

    Code:
    #include<stdio.h>
    
    int main()
    {
        int i = 10,
            n = 1;
        int j;
        
        while(i >= n)
            {
            j = 1;
            printf("\n");
            while(j <= i)
            {    
                 if(i == 10)
                    printf("0");
                 else
                    printf("%d",i);
                 j++;
            }
                 i--;
            }
        return 0;
    }
    and this will produce output:

    0000000000
    999999999
    88888888
    7777777
    666666
    55555
    4444
    333
    22
    1

    like g3tbombed expected

    but

    if i using global variables outside the loop like this

    Code:
    #include<stdio.h>
    
    int main()
    {
        int i  = 10,
            n = 1;
        int j = 1;
        
        while(i >= n)
            {
        //  j = 1;          /*Erase This and Use Global Variables Outside Loop*/
            printf("\n");
            while(j <= i)
            {    
                 if(i == 10)
                    printf("0");
                 else
                    printf("%d",i);
                 j++;
            }
                 i--;
            }
        return 0;
    }
    and this will produce output:

    0000000000
    and escape sequence '\n' until the outer loop finished

    why does this happened? its variable on loop are sensitive case that only use on local variables on loop.
    PS: I just want to try, no building him the assignment for him, sorry if i posting this because this will answering the assignment for him.

    Thank you in advance.

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Because every-time you were entering the while in line 9, j was getting the value 1. Now you are erased it and placed it before while loop, thus it gets the value 1 only once.

    Terminology hazard: j is not a global variable.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  14. #14
    Registered User loserone+_+'s Avatar
    Join Date
    Dec 2012
    Location
    Indonesia
    Posts
    112
    i see, so simply said, its only used once on inner loop because J didnt get loop from outer loop ?
    i mean repeated j = 1 in outer loop.

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes, j will get the value of 1, every time we enter the outer loop.
    (in the first code)
    However, in the second, j will get value 1, only once, before entering the outer loop.

    You see the difference is that every line of code that is in the loop, gets executed every-time the loop is been executed.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 09-02-2013, 09:11 AM
  2. Creating Objects in Loops
    By MattMik in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2008, 11:12 AM
  3. Creating a removeVowel function for Loops...
    By wco5002 in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2007, 12:50 AM
  4. Simple C Program [for loops?]
    By mas0 in forum C Programming
    Replies: 6
    Last Post: 11-18-2006, 05:53 PM
  5. program loops twice
    By ruffle in forum C Programming
    Replies: 6
    Last Post: 11-17-2005, 01:02 PM

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